一个Java的Restful服务框架,支持JPA、JAAS、分布式资源对象

项目地址: http://code.google.com/p/jrest4guice/
Demo演示: http://cnoss.vicp.net/


当前版本:0.9.0 preview

特点:
  • 基于Google guice
  • 零配置,服务的自动扫描注册
  • 非侵入式,用户不需要实现特定的接口来实现Restful服务
  • 支持Post. Get. Put. Delete操作
  • 灵活的注入(支持上下文环境request/response/session以及参数的自动注入)
  • 根据客户端要求返回不同类型的数据(xml/json/html)
  • 支持Velocity、Freemarker和Spry模板引擎(当返回类型是text/html时才有效,参见@ViewTemplate)
  • 支持JPA,通过增强的BaseEntityManager实现实体的CRUD
  • 支持事务,通过@Transactional注解声明事务的类型
  • 支持JAAS,通过@RolesAllowed注解声明操作所需要的角色
  • 支持分布式资源对象,实现业务逻辑的分布式部署


下一步计划:

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


代码示例:

Java代码 复制代码
  1. /**  
  2.  *   
  3.  * @author <a href="mailto:[email protected]">cnoss (QQ:86895156)</a>  
  4.  * 联系人的资源对象,并声明为Remote(可以通过@RemoteService的注入到任一资源对象,通常用在跨应用的资源调用上)  
  5.  */  
  6. @Path( { "/contact""/contacts/{contactId}" })   
  7. @Remote  
  8. public class ContactResource {   
  9.     @Inject  
  10.     private ContactService service;   
  11.   
  12.     /**  
  13.      * 创建新的联系人  
  14.      * contact 联系人实体  
  15.      */  
  16.     @Post  
  17.     public String createContact(@ModelBean Contact contact) {   
  18.         return this.service.createContact(contact);   
  19.     }   
  20.   
  21.     /**  
  22.      * 修改联系人信息  
  23.      * contact 联系人实体  
  24.      */  
  25.     @Put  
  26.     public void putContact(@ModelBean Contact contact) {   
  27.         this.service.updateContact(contact);   
  28.     }   
  29.   
  30.     /**  
  31.      * 显示联系人列表,并限定服务所支持的返回数据类型只能为application/json和application/javabean  
  32.      * pageIndex 页码  
  33.      * pageSize 每页记录数  
  34.      */  
  35.     @Get  
  36.     @ProduceMime( {MimeType.MIME_OF_JSON,MimeType.MIME_OF_JAVABEAN})   
  37.     @Path("/contacts")   
  38.     public Page<Contact> listContacts(int pageIndex, int pageSize) {   
  39.         return this.service.listContacts(pageIndex, pageSize);   
  40.     }   
  41.   
  42.     /**  
  43.      * 显示单个联系人的信息  
  44.      * 并指定了当客户端请求类型为text/html时的视图显示模板(现在系统内置对Velocity、Freemarker与Spry的支持)  
  45.      * contactId 联系对象ID  
  46.      */  
  47.     @Get  
  48.     @ViewTemplate(url="/template/contactDetail.vm",render=ViewRenderType.VELOCITY)   
  49. //  @ViewTemplate(url="/template/contactDetail.ftl",render=ViewRenderType.FREEMARKER)   
  50. //  @ViewTemplate(url="/template/contactDetail.html",render=ViewRenderType.SPRY)   
  51.     public Contact getContact(@Parameter("contactId") String contactId) {   
  52.         return this.service.findContactById(contactId);   
  53.     }   
  54.   
  55.     /**  
  56.      * 删除指定ID的联系人  
  57.      * contactId 联系对象ID  
  58.      */  
  59.     @Delete  
  60.     public void deleteContact(@Parameter("contactId") String contactId) {   
  61.         this.service.deleteContact(contactId);   
  62.     }   
  63. }   
  64.   
  65. /**  
  66.  * 业务对象  
  67.  * @author <a href="mailto:[email protected]">cnoss (QQ:86895156)</a>  
  68.  *  
  69.  */  
  70. @SuppressWarnings({"unchecked""unused"})   
  71. public class ContactServiceBean implements ContactService {   
  72.   
  73.     private BaseEntityManager<String, Contact> entityManager;   
  74.   
  75.     @Inject  
  76.     private void init(EntityManager em) {   
  77.         this.entityManager = new BaseEntityManager<String, Contact>(   
  78.                 Contact.class, em);   
  79.     }   
  80.   
  81.     @Transactional  
  82.     @RolesAllowed({"user"})//权限声明   
  83.     public String createContact(Contact contact) {   
  84.         if (contact == null)   
  85.             throw new RuntimeException("联系人对象不能为空");   
  86.   
  87.         if (this.entityManager.loadByNamedQuery("byName", contact.getName()) != null) {   
  88.             throw new RuntimeException("联系人的姓名相同,请重新输入");   
  89.         }   
  90.   
  91.         this.entityManager.create(contact);   
  92.         return contact.getId();   
  93.     }   
  94.   
  95.     @Transactional  
  96.     @RolesAllowed({"admin","audit"})//权限声明   
  97.     public void deleteContact(String contactId) {   
  98.         String[] ids = contactId.split(",");   
  99.         Contact contact;   
  100.         for (String id : ids) {   
  101.             contact = this.findContactById(id);   
  102.             if (contact == null)   
  103.                 throw new RuntimeException("联系人不存在");   
  104.             this.entityManager.delete(contact);   
  105.         }   
  106.     }   
  107.   
  108.     @Transactional(type=TransactionalType.READOLNY)//只读性的事务   
  109.     public Contact findContactById(String contactId) {   
  110.         return this.entityManager.load(contactId);   
  111.     }   
  112.   
  113.     @Transactional(type=TransactionalType.READOLNY)   
  114.     public Page<Contact> listContacts(int pageIndex, int pageSize)   
  115.             throws RuntimeException {   
  116.         return this.entityManager.pageByNamedQuery("list",   
  117.                 new Pagination(pageIndex, pageSize));   
  118.     }   
  119.   
  120.     @Transactional  
  121.     @RolesAllowed({"admin","user"})//权限声明   
  122.     public void updateContact(Contact contact) {   
  123.         if (contact == null)   
  124.             throw new RuntimeException("联系人对象不能为空");   
  125.   
  126.         this.entityManager.update(contact);   
  127.     }   
  128. }   
  129.   
  130.   
  131. /**  
  132.  * 远程资源引用的示例  
  133.  * @author <a href="mailto:[email protected]">cnoss (QQ:86895156)</a>  
  134.  *   
  135.  */  
  136. @Path({"/testCallRemote"})   
  137. public class TestRemoteResource {   
  138.     @Inject  
  139.     @RemoteService //注入远程资源对象的引用   
  140.     private ContactResource service;   
  141.   
  142.     @Get  
  143.     @ProduceMime( {MimeType.MIME_OF_JSON,MimeType.MIME_OF_JAVABEAN})   
  144.     public Page<Contact> listContacts(int pageIndex, int pageSize) {   
  145.         return this.service.listContacts(pageIndex, pageSize);   
  146.     }   
  147. }  
/**
 * 
 * @author <a href="mailto:[email protected]">cnoss (QQ:86895156)</a>
 * 联系人的资源对象,并声明为Remote(可以通过@RemoteService的注入到任一资源对象,通常用在跨应用的资源调用上)
 */
@Path( { "/contact", "/contacts/{contactId}" })
@Remote
public class ContactResource {
	@Inject
	private ContactService service;

	/**
	 * 创建新的联系人
	 * contact 联系人实体
	 */
	@Post
	public String createContact(@ModelBean Contact contact) {
		return this.service.createContact(contact);
	}

	/**
	 * 修改联系人信息
	 * contact 联系人实体
	 */
	@Put
	public void putContact(@ModelBean Contact contact) {
		this.service.updateContact(contact);
	}

	/**
	 * 显示联系人列表,并限定服务所支持的返回数据类型只能为application/json和application/javabean
	 * pageIndex 页码
	 * pageSize 每页记录数
	 */
	@Get
	@ProduceMime( {MimeType.MIME_OF_JSON,MimeType.MIME_OF_JAVABEAN})
	@Path("/contacts")
	public Page<Contact> listContacts(int pageIndex, int pageSize) {
		return this.service.listContacts(pageIndex, pageSize);
	}

	/**
	 * 显示单个联系人的信息
	 * 并指定了当客户端请求类型为text/html时的视图显示模板(现在系统内置对Velocity、Freemarker与Spry的支持)
	 * contactId 联系对象ID
	 */
	@Get
	@ViewTemplate(url="/template/contactDetail.vm",render=ViewRenderType.VELOCITY)
//	@ViewTemplate(url="/template/contactDetail.ftl",render=ViewRenderType.FREEMARKER)
//	@ViewTemplate(url="/template/contactDetail.html",render=ViewRenderType.SPRY)
	public Contact getContact(@Parameter("contactId") String contactId) {
		return this.service.findContactById(contactId);
	}

	/**
	 * 删除指定ID的联系人
	 * contactId 联系对象ID
	 */
	@Delete
	public void deleteContact(@Parameter("contactId") String contactId) {
		this.service.deleteContact(contactId);
	}
}

/**
 * 业务对象
 * @author <a href="mailto:[email protected]">cnoss (QQ:86895156)</a>
 *
 */
@SuppressWarnings({"unchecked", "unused"})
public class ContactServiceBean implements ContactService {

	private BaseEntityManager<String, Contact> entityManager;

	@Inject
	private void init(EntityManager em) {
		this.entityManager = new BaseEntityManager<String, Contact>(
				Contact.class, em);
	}

	@Transactional
	@RolesAllowed({"user"})//权限声明
	public String createContact(Contact contact) {
		if (contact == null)
			throw new RuntimeException("联系人对象不能为空");

		if (this.entityManager.loadByNamedQuery("byName", contact.getName()) != null) {
			throw new RuntimeException("联系人的姓名相同,请重新输入");
		}

		this.entityManager.create(contact);
		return contact.getId();
	}

	@Transactional
	@RolesAllowed({"admin","audit"})//权限声明
	public void deleteContact(String contactId) {
		String[] ids = contactId.split(",");
		Contact contact;
		for (String id : ids) {
			contact = this.findContactById(id);
			if (contact == null)
				throw new RuntimeException("联系人不存在");
			this.entityManager.delete(contact);
		}
	}

	@Transactional(type=TransactionalType.READOLNY)//只读性的事务
	public Contact findContactById(String contactId) {
		return this.entityManager.load(contactId);
	}

	@Transactional(type=TransactionalType.READOLNY)
	public Page<Contact> listContacts(int pageIndex, int pageSize)
			throws RuntimeException {
		return this.entityManager.pageByNamedQuery("list",
				new Pagination(pageIndex, pageSize));
	}

	@Transactional
	@RolesAllowed({"admin","user"})//权限声明
	public void updateContact(Contact contact) {
		if (contact == null)
			throw new RuntimeException("联系人对象不能为空");

		this.entityManager.update(contact);
	}
}


/**
 * 远程资源引用的示例
 * @author <a href="mailto:[email protected]">cnoss (QQ:86895156)</a>
 * 
 */
@Path({"/testCallRemote"})
public class TestRemoteResource {
	@Inject
	@RemoteService //注入远程资源对象的引用
	private ContactResource service;

	@Get
	@ProduceMime( {MimeType.MIME_OF_JSON,MimeType.MIME_OF_JAVABEAN})
	public Page<Contact> listContacts(int pageIndex, int pageSize) {
		return this.service.listContacts(pageIndex, pageSize);
	}
}



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

真诚希望大家提出宝贵意见,联系方式:

你可能感兴趣的:(java,框架,json,qq,jpa)