向大家推荐一个轻量级的java rest 框架 JRest4Guice

[size=x-large][color=red]本帖已转向:[url]http://www.iteye.com/topic/201103[/url][/color][/size]


[i]
大家好,今天向大家推荐一个轻量级的java rest 框架 JRest4Guice

项目地址: [url]http://code.google.com/p/jrest4guice/[/url]

这个项目借鉴了[url]http://www.iteye.com/topic/170289[/url]的一些思想和代码。本人在此先谢了。

特点:
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();
}
}
}
[/i]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值