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

本帖已转向:http://www.iteye.com/topic/201103


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

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

这个项目借鉴了 http://www.iteye.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();
		}
	}
}
[/i]

你可能感兴趣的:(java,框架,REST,嵌入式,Rails)