tapestry restful架构使用

来源:互联网 发布:js设计模式6大原则 编辑:程序博客网 时间:2024/05/29 16:29

restful架构的好处就不介绍了, tapestry5如何好也不说,用的人都知道。怎么把tapestry+rest结合?

very easy,请继续look!

版本,目前了解,tapestry-resteasy-0.0.1版本配置比较繁琐,而且需要修改web.xml文件不建议使用。

tapestry-resteasy-0.0.1详细配置教程请看http://tynamo.org/tapestry-resteasy-0.0.1+guide。


本人使用的是tapestry-resteasy-0.4.0版本
默认tapestry已经部署好了。默认了解maven
此配置方式不会影响原来tapestry的任何配置,只要添加几个支持restful架构的配置即可。两个字-“完美”


1,Add the tapestry-resteasy dependency to your pom.xml
添加tapstry-resteasy 的所有依赖到你maven项目的pom.xml
 <dependency>
            <groupId>org.tynamo</groupId>
            <artifactId>tapestry-resteasy</artifactId>
            <version>0.4.0</version>
 </dependency>
本人使用的tapestry版本是5.3.7。
2,No need to edit your web.xml.
tapestry-resteasy integrates with the Tapestry's HttpServletRequest pipeline.
0.0.1后面的版本都不需要再编辑web.xml了。因为tapestry-resteasy 整合进了tapestry的HttpServletRequest 的管道流里了
具体原理请期待后期整理。


3,Create a package named "rest" for your rest services.
在使用tapestry时会在web.xml中有段配置
<context-param>
<param-name>tapestry.app-package</param-name>
<param-value>xxx.com.foodorder.core</param-value>
</context-param>
此时你要在xxx.com.foodorder.core包下创建一个rest子包。这样tapestry启动时会自动扫描该目录。
如果你自己已经写了一下restful api在其他包下可以在appMoudle里添加该方法:
public static void contributeResteasyPackageManager(Configuration<String> configuration)
{
configuration.add("你的包名");
}
4,Code your singleton resource. 
rest api的资源类:


import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import org.apache.tapestry5.json.JSONObject;
import zte.com.foodorder.service.services.UserService;


@Path("/mydomainobject")
public class MyDomainObjectResource
{


@Inject
private UserService userService;




@GET
@Produces({ "application/json"})
public Object getAllDomains()
{
// System.out.println(userService.all().size());
JSONObject json = new JSONObject();
json.put("success",false);
json.put("msg","nologin");
return json.toString();
}


}


5,此时你的项目就已经支持rest架构了。
访问地址是:http://localhost:8080/rest/mydomainobject/
mydomainobject是@Path中配置的"/mydomainobject",默认要加rest




tapestry就是这么赞,集成任何框架都是快快快。

0 0
原创粉丝点击