声明:JavaEye新闻文章的版权属于JavaEye网站所有,严禁任何网站转载本文,否则必将追究法律责任!
大家好,今天向大家推荐一个轻量级的java rest 框架 JRest4Guice
项目地址: http://code.google.com/p/jrest4guice/
这个项目借鉴了http://www.javaeye.com/topic/170289的一些思想和代码。本人在些先谢了。
特点:
1. 基于GUICE
2. 零配置式服务声明
@Restful(uri = "/contacts")
public class ContactListRestService
3. 服务的自动扫描注册
4. 非侵入式风格,用户不需要实现特定的接口来实现Restful服务
用户只要在指定的POJO上:
1. 声明为@Restful,并指明访问的URI格式
2. 在指定的方法上声明为@HttpMethod
5. 支持Rest的Post. Get. Put. Delete操作
用户在指定的方法上通过@HttpMethod注解来声明方法的类型,如下:
@HttpMethod(type = HttpMethodType.POST)
public String createContact(String name, @RequestParameter("homePhone") String homePhone, @ModelBean Contact contact)
@HttpMethod
public String getContact(@FirstResult int first, @MaxResults int max)
注:如果没有提供HttpMethodType类型的声明,系统会自动根据方法名称的前缀来自动识别(方法名必须以get/post/put/delete开头)
6. 灵活的注入
6.1. 支持HttpServletRequest. HttpServletResponse. ModelMap的注入
@Inject
private ModelMap modelMap;
@Inject
private HttpServletRequest request;
@Inject
private HttpServletResponse response;
6.2. 支持参数的自动注入
方法中的参数可以由系统自动注入,如下:
public String createContact(String name, @RequestParameter("homePhone") String homePhone, @ModelBean Contact contact)
注:如果参数没有任何注解,系统默认获取上下文ID为参数名称的参数值,否则通过@RequestParameter注解指定的参数名称来获取,@ModelBean可以将上下文中的参数转换成指定参数类型的Java bean
6.3. 支持对JndiResource的注入
示例代码:
项目地址: http://code.google.com/p/jrest4guice/
这个项目借鉴了http://www.javaeye.com/topic/170289的一些思想和代码。本人在些先谢了。
特点:
1. 基于GUICE
2. 零配置式服务声明
@Restful(uri = "/contacts")
public class ContactListRestService
3. 服务的自动扫描注册
4. 非侵入式风格,用户不需要实现特定的接口来实现Restful服务
用户只要在指定的POJO上:
1. 声明为@Restful,并指明访问的URI格式
2. 在指定的方法上声明为@HttpMethod
5. 支持Rest的Post. Get. Put. Delete操作
用户在指定的方法上通过@HttpMethod注解来声明方法的类型,如下:
@HttpMethod(type = HttpMethodType.POST)
public String createContact(String name, @RequestParameter("homePhone") String homePhone, @ModelBean Contact contact)
@HttpMethod
public String getContact(@FirstResult int first, @MaxResults int max)
注:如果没有提供HttpMethodType类型的声明,系统会自动根据方法名称的前缀来自动识别(方法名必须以get/post/put/delete开头)
6. 灵活的注入
6.1. 支持HttpServletRequest. HttpServletResponse. ModelMap的注入
@Inject
private ModelMap modelMap;
@Inject
private HttpServletRequest request;
@Inject
private HttpServletResponse response;
6.2. 支持参数的自动注入
方法中的参数可以由系统自动注入,如下:
public String createContact(String name, @RequestParameter("homePhone") String homePhone, @ModelBean Contact contact)
注:如果参数没有任何注解,系统默认获取上下文ID为参数名称的参数值,否则通过@RequestParameter注解指定的参数名称来获取,@ModelBean可以将上下文中的参数转换成指定参数类型的Java bean
6.3. 支持对JndiResource的注入
示例代码:
@Restful(uri = { "/contact", "/contact/{contactId}" })
public class ContactRestService {
@Inject
private ModelMap modelMap;
@Inject
private HttpServletRequest request;
@Inject
private HttpServletResponse response;
@Inject
@JndiResource(jndi = "test/ContactService")
private ContactService service;
@HttpMethod(type = HttpMethodType.POST)
public String createContact(String name, @RequestParameter("homePhone") String homePhone, @ModelBean Contact contact) {
if (contact == null)
return HttpResult.createFailedHttpResult("-1","联系人信息不能为空").toJson();
String contactId = null;
try {
contactId = this.service.createContact(contact);
return HttpResult.createSuccessfulHttpResult(contactId).toJson();
} catch (RemoteException e) {
return HttpResult.createFailedHttpResult(e.getClass().getName(),e.getMessage()).toJson();
}
}
@HttpMethod
public String putContact(@RequestParameter("contactId")
String contactId, @ModelBean
Contact contact) {
if (contactId == null)
return HttpResult.createFailedHttpResult("-1","没有指定对应的联系人标识符").toJson();
try {
this.service.updateContact(contact);
return HttpResult.createSuccessfulHttpResult("修改成功").toJson();
} catch (RemoteException e) {
return HttpResult.createFailedHttpResult(e.getClass().getName(),e.getMessage()).toJson();
}
}
@HttpMethod
public String getContact(@RequestParameter("contactId")
String contactId) {
try {
Contact contactDto = this.service.findContactById(contactId);
return HttpResult.createSuccessfulHttpResult(contactDto).toJson();
} catch (Exception e) {
return HttpResult.createFailedHttpResult(e.getClass().getName(),e.getMessage()).toJson();
}
}
@HttpMethod
public String deleteContact(@RequestParameter("contactId")
String contactId) {
try {
this.service.deleteContact(contactId);
return HttpResult.createSuccessfulHttpResult("删除成功").toJson();
} catch (Exception e) {
return HttpResult.createFailedHttpResult(e.getClass().getName(),e.getMessage()).toJson();
}
}
}


评论 共 5 条 发表评论
cnoss 2008-03-29 22:51
1、业务接口
@ImplementedBy(ContactServiceBean.class) public interface ContactService { public String createContact(Contact contact) throws RemoteException; public List<Contact> listContacts(int first,int max) throws RemoteException; public Contact findContactById(String contactId) throws RemoteException; public void updateContact(Contact contact) throws RemoteException; public void deleteContact(String contactId) throws RemoteException; }2、业务实现
public class ContactServiceBean implements ContactService { @Inject private EntityManager entityManager;//JPA实体管理器的注入 @Transactional//事务声明 public String createContact(Contact contact) throws RemoteException { if (contact == null) throw new RemoteException("联系人的内容不能为空"); if(this.entityManager.createNamedQuery("byName").setParameter("name", contact.getName()).getResultList().size()>0){ throw new RemoteException("联系人的姓名相同,请重新输入"); } this.entityManager.persist(contact); return contact.getId(); } @Transactional//事务声明 public void deleteContact(String contactId) throws RemoteException { Contact contact = this.findContactById(contactId); if (contact == null) throw new RemoteException("联系人不存在"); this.entityManager.remove(contact); } @Transactional//事务声明 public Contact findContactById(String contactId) throws RemoteException { return this.entityManager.find(Contact.class, contactId); } @Transactional//事务声明 public List<Contact> listContacts(int first, int max) throws RemoteException { return this.entityManager.createNamedQuery("list").setFirstResult(first) .setMaxResults(max).getResultList(); } @Transactional//事务声明 public void updateContact(Contact contact) throws RemoteException { if (contact == null) throw new RemoteException("联系人的内容不能为空"); this.entityManager.merge(contact); } }3、Rest的控制类
@Restful(uri = { "/contact", "/contact/{contactId}" })
public class ContactController {
@Inject
private ContactService service;//注入的业务接口
@HttpMethod(type = HttpMethodType.POST)
public String createContact(String name, @RequestParameter("homePhone") String homePhone, @ModelBean Contact contact) {}
...
...
}
===========================================
我们会在下一个版本中增加动态DAO的支持
===========================================
注:
1、由于最近的更新比较频繁,所以没有打包,有需要的朋友请从SVN上直接下载原代码,源代码中有详细的例子。
2、示例已经可以运行jboss/tomcat/jetty服务器上,已经完全脱离了EJB环境。
谢谢大家的参与和批评。
-- cnoss小组
saintlu 2008-03-28 14:58
cnoss 2008-03-27 22:13
moonranger 2008-03-27 21:03
我特别期待以Guice,Warp为代表的一批新框架的“崛起”。也许SSH在大部分场合还是最佳选择,但是我还是希望能在小型项目中获得Rails式的优雅和开发效率。
我觉得这种探索是绝对有意义的!
crebox 2008-03-27 19:36