JRest4Guice

项目地址: http://code.google.com/p/jrest4guice/ 
Demo演示: http://www.rest4g.org/full
 


http://www.javaeye.com/topic/201103 一个Java的Restful服务框架,支持JPA、JAAS、分布式资源对象 项目地址:http://code.google.com/p/jrest4guice/   一个轻量级的基于Google Guice的Restful服务框架,支持JPA、JAAS、分布式资源对象,对OSGI的支持也在计划中. (Demo演示: http://cnoss.vicp.net/ ) 请大家直接从SVN中获取JRest4Guice工程即可 svn checkout http://jrest4guice.googlecode.com/svn/trunk/ jrest4guice-read-only (使用Maven)


当前版本:0.9.0 preview 

特点: 

  • 基于Google guice
  • 零配置,服务的自动扫描注册
  • 非侵入式,用户不需要实现特定的接口来实现Restful服务,只需要通过@RESTful来声明
  • 支持Post. Get. Put. Delete操作
  • 支持对Get操作的缓存机制,实现动态资源静态化(通过@Cache标注声明)
  • 灵活的注入(支持上下文环境request/response/session以及参数的自动注入)
  • 根据客户端要求返回不同类型的数据(xml/json/html)
  • 通过@PageFlow实现对MVC module2的支持,输出结果支持CTE、Velocity、Freemarker和Spry模板引擎(当返回类型是text/html时才有效)
  • 支持JPA,通过增强的BaseEntityManager实现实体的CRUD
  • 支持事务,通过@Transactional注解声明事务的类型
  • 支持JAAS,通过@RolesAllowed注解声明操作所需要的角色
  • 支持Hibernate validator
  • 支持拦截器 (interceptor)
  • 支持分布式资源对象,实现业务逻辑的分布式部署
  • 提供了与Struts2集成的插件


下一步计划:

  • OSGI的支持
  • 分布式事务的支持


代码示例:

Java代码    收藏代码
  1. //=======================================================  
  2. //资源类  
  3. //=======================================================  
  4.   
  5. /** 
  6.  * @author <a href="mailto:zhangyouqun@gmail.com">cnoss (QQ:86895156)</a> 
  7.  * 联系人的资源对象 
  8.  * 声明remoteable为真(可以通过@RemoteReference的注入到任一资源对象,通常用在跨应用的资源调用上) 
  9.  */  
  10. @RESTful(name = "ContactResource", remoteable = true)  
  11. @Path( { "/contact""/contacts/{contactId}" })  
  12. public class ContactResource {  
  13.     @Inject  
  14.     private ContactService service;//注入联系人管理的服务对象  
  15.   
  16.     /** 
  17.      * 创建新的联系人  
  18.      * PageFlow :当服务端返回类型是Text/Html类型时,重定向用户的请求到指定的页面,实现最基本功能的MVC。 
  19.      *      在这里,指明当操作成功时,重定向到/contacts,当操作失败时,将用户请求重定向到/contact。 
  20.      * @param contact 联系人实体 
  21.      */  
  22.     @Post  
  23.     @PageFlow(success = @PageInfo(value = "/contacts",type=ResultType.REDIRECT)  
  24.             ,error=@PageInfo(value="/contact",type=ResultType.REDIRECT))  
  25.     public String createContact(@ModelBean Contact contact) {  
  26.         return this.service.createContact(contact);  
  27.     }  
  28.   
  29.     /** 
  30.      * 修改联系人信息  
  31.      * @param contact 联系人实体 
  32.      */  
  33.     @Put  
  34.     @PageFlow(success = @PageInfo(value = "/contacts",type=ResultType.REDIRECT)  
  35.             ,error=@PageInfo(value="/contact",type=ResultType.REDIRECT))  
  36.     public void putContact(@ModelBean Contact contact) {  
  37.         this.service.updateContact(contact);  
  38.     }  
  39.   
  40.     /** 
  41.      * 显示联系人列表  
  42.      * @param page 页码  
  43.      * @param size 每页记录数 
  44.      */  
  45.     @Get  
  46.     @Path("/contacts")  
  47.     @PageFlow(success = @PageInfo(value = "/template/contacts.ctl"))  
  48.     public Page<Contact> listContacts(int page, int size) {  
  49.         return this.service.listContacts(page, size);  
  50.     }  
  51.   
  52.     /** 
  53.      * 显示单个联系人的信息  
  54.      * @param contactId 联系对象ID 
  55.      */  
  56.     @Get  
  57.     @PageFlow(success = @PageInfo(value = "/template/contactDetail.ctl"))  
  58.     public Contact getContact(@Parameter("contactId") String contactId) {  
  59.         if(contactId == null)  
  60.             return new Contact();  
  61.         return this.service.findContactById(contactId);  
  62.     }  
  63.   
  64.     /** 
  65.      * 删除指定ID的联系人  
  66.      * @param contactId 联系对象ID 
  67.      */  
  68.     @Delete  
  69.     @PageFlow(success = @PageInfo(value = "/contacts",type=ResultType.REDIRECT))  
  70.     public void deleteContact(@Parameter("contactId") String contactId) {  
  71.         this.service.deleteContact(contactId);  
  72.     }  
  73. }  
  74.   
  75.   
  76. //=======================================================  
  77. //业务类  
  78. //=======================================================  
  79.   
  80. /** 
  81.  *  
  82.  * @author <a href="mailto:zhangyouqun@gmail.com">cnoss (QQ:86895156)</a> 
  83.  * 
  84.  */  
  85. @Transactional//事务支持,缺省为TransactionalType.REQUIRED,可以在方法中覆写  
  86. @Interceptors({//自定义的拦截器(类级别的,作用于所有的方法,可以在方法中覆写)  
  87.     @Interceptor(TestInterceptor.class),  
  88.     @Interceptor(LogInterceptor.class)  
  89. })  
  90. public class ContactService{  
  91.     //注入实体管理器  
  92.     @Inject  
  93.     private BaseEntityManager<String, Contact> entityManager;  
  94.   
  95.     public String createContact(Contact contact) {  
  96.         if (contact == null)  
  97.             throw new RuntimeException("联系人的内容不能为空");  
  98.   
  99.         if (this.entityManager.loadByNamedQuery("byName", contact.getName()) != null) {  
  100.             throw new RuntimeException("联系人的姓名相同,请重新输入");  
  101.         }  
  102.   
  103.         this.entityManager.create(contact);  
  104.         return contact.getId();  
  105.     }  
  106.   
  107.     public void deleteContact(String contactId) {  
  108.         String[] ids = contactId.split(",");  
  109.         Contact contact;  
  110.         for (String id : ids) {  
  111.             contact = this.findContactById(id);  
  112.             if (contact == null)  
  113.                 throw new RuntimeException("联系人不存在");  
  114.             this.entityManager.delete(contact);  
  115.         }  
  116.     }  
  117.   
  118.     @Transactional(type=TransactionalType.READOLNY)  
  119.     public Contact findContactById(String contactId) {  
  120.         return this.entityManager.load(contactId);  
  121.     }  
  122.   
  123.     @Transactional(type=TransactionalType.READOLNY)//覆盖类级别的事务类型为只读  
  124.     @Interceptor(ListContactsInterceptor.class)//覆盖类级别的拦截器  
  125.     public Page<Contact> listContacts(int pageIndex, int pageSize)  
  126.             throws RuntimeException {  
  127.         return this.entityManager.pageByNamedQuery("list",  
  128.                 new Pagination(pageIndex, pageSize));  
  129.     }  
  130.   
  131.     public void updateContact(Contact contact) {  
  132.         if (contact == null)  
  133.             throw new RuntimeException("联系人的内容不能为空");  
  134.           
  135.         Contact tmpContact = this.entityManager.loadByNamedQuery("byName", contact.getName());  
  136.         if(tmpContact != null && !contact.getId().equals(tmpContact.getId()))  
  137.             throw new RuntimeException("联系人的姓名相同,请重新输入");  
  138.   
  139.         this.entityManager.update(contact);  
  140.     }  
  141. }  
  142.   
  143.   
  144. //=======================================================  
  145. //远程调用的案例  
  146. //=======================================================  
  147.   
  148. /** 
  149.  *  
  150.  * @author <a href="mailto:zhangyouqun@gmail.com">cnoss (QQ:86895156)</a> 
  151.  * 
  152.  */  
  153. @Path( { "/testCallRemote"})  
  154. public class TestRemoteResource {  
  155.     @Inject  
  156.     @RemoteReference//注入远程资源对象  
  157.     private ContactResource service;  
  158.   
  159.     @Get  
  160.     public Page<Contact> listContacts(int page, int size) {  
  161.         return this.service.listContacts(page, size);  
  162.     }  
  163. }  

 

 

请大家直接从SVN中获取JRest4Guice、JRest4Guice-sample、libraries三个工程即可 

 

http://jrest4guice.googlecode.com/files/JRest4Guice-0.8.1_Jpa4Guice-0.1-with-example.zip

http://jrest4guice.googlecode.com/files/JRest4Guice-0.8.1-with-example.zip

http://jrest4guice.googlecode.com/files/JRest4Guice.rar

http://jrest4guice.googlecode.com/svn/trunk/

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值