阅读更多

8顶
0踩

编程语言
大家好,今天向大家推荐一个轻量级的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();
		}
	}
}
8
0
评论 共 5 条 请登录后发表评论
5 楼 cnoss 2008-03-29 22:51
经过几天的奋战,我们团队又发布了一个基于Guice的JPA实现--Jpa4Guice0.1 预览版,示例如下:

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小组
4 楼 saintlu 2008-03-28 14:58
看起来不错。
3 楼 cnoss 2008-03-27 22:13
有兴趣的朋友请从http://code.google.com/p/jrest4guice/ 获取最新代码,打包中的例子使用了EJB3,我只在Jboss环境下测试通过。访问地址:http://localhost:8080/JRest4Guice/ 是一个简单的有关联系的CRUD操作
2 楼 moonranger 2008-03-27 21:03
我的那个东西也只是一时兴起之作,你做得比我更好,学习一下。
我特别期待以Guice,Warp为代表的一批新框架的“崛起”。也许SSH在大部分场合还是最佳选择,但是我还是希望能在小型项目中获得Rails式的优雅和开发效率。
我觉得这种探索是绝对有意义的!
1 楼 crebox 2008-03-27 19:36
学习一下

发表评论

您还没有登录,请您登录后再发表评论

相关推荐

Global site tag (gtag.js) - Google Analytics