|
该帖已经被评为精华帖
|
|
|---|---|
| 作者 | 正文 |
|
最后更新时间:2008-06-16 关键字: rest
项目地址: http://code.google.com/p/jrest4guice/
Demo演示: http://www.rest4g.org/full 当前版本:0.9.0 preview 特点:
下一步计划:
代码示例:
//=======================================================
//资源类
//=======================================================
/**
* @author <a href="mailto:zhangyouqun@gmail.com">cnoss (QQ:86895156)</a>
* 联系人的资源对象
* 声明remoteable为真(可以通过@RemoteReference的注入到任一资源对象,通常用在跨应用的资源调用上)
*/
@RESTful(name = "ContactResource", remoteable = true)
@Path( { "/contact", "/contacts/{contactId}" })
public class ContactResource {
@Inject
private ContactService service;//注入联系人管理的服务对象
/**
* 创建新的联系人
* PageFlow :当服务端返回类型是Text/Html类型时,重定向用户的请求到指定的页面,实现最基本功能的MVC。
* 在这里,指明当操作成功时,重定向到/contacts,当操作失败时,将用户请求重定向到/contact。
* @param contact 联系人实体
*/
@Post
@PageFlow(success = @PageInfo(value = "/contacts",type=ResultType.REDIRECT)
,error=@PageInfo(value="/contact",type=ResultType.REDIRECT))
public String createContact(@ModelBean Contact contact) {
return this.service.createContact(contact);
}
/**
* 修改联系人信息
* @param contact 联系人实体
*/
@Put
@PageFlow(success = @PageInfo(value = "/contacts",type=ResultType.REDIRECT)
,error=@PageInfo(value="/contact",type=ResultType.REDIRECT))
public void putContact(@ModelBean Contact contact) {
this.service.updateContact(contact);
}
/**
* 显示联系人列表
* @param page 页码
* @param size 每页记录数
*/
@Get
@Path("/contacts")
@PageFlow(success = @PageInfo(value = "/template/contacts.ctl"))
public Page<Contact> listContacts(int page, int size) {
return this.service.listContacts(page, size);
}
/**
* 显示单个联系人的信息
* @param contactId 联系对象ID
*/
@Get
@PageFlow(success = @PageInfo(value = "/template/contactDetail.ctl"))
public Contact getContact(@Parameter("contactId") String contactId) {
if(contactId == null)
return new Contact();
return this.service.findContactById(contactId);
}
/**
* 删除指定ID的联系人
* @param contactId 联系对象ID
*/
@Delete
@PageFlow(success = @PageInfo(value = "/contacts",type=ResultType.REDIRECT))
public void deleteContact(@Parameter("contactId") String contactId) {
this.service.deleteContact(contactId);
}
}
//=======================================================
//业务类
//=======================================================
/**
*
* @author <a href="mailto:zhangyouqun@gmail.com">cnoss (QQ:86895156)</a>
*
*/
@Transactional//事务支持,缺省为TransactionalType.REQUIRED,可以在方法中覆写
@Interceptors({//自定义的拦截器(类级别的,作用于所有的方法,可以在方法中覆写)
@Interceptor(TestInterceptor.class),
@Interceptor(LogInterceptor.class)
})
public class ContactService{
//注入实体管理器
@Inject
private BaseEntityManager<String, Contact> entityManager;
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();
}
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)//覆盖类级别的事务类型为只读
@Interceptor(ListContactsInterceptor.class)//覆盖类级别的拦截器
public Page<Contact> listContacts(int pageIndex, int pageSize)
throws RuntimeException {
return this.entityManager.pageByNamedQuery("list",
new Pagination(pageIndex, pageSize));
}
public void updateContact(Contact contact) {
if (contact == null)
throw new RuntimeException("联系人的内容不能为空");
Contact tmpContact = this.entityManager.loadByNamedQuery("byName", contact.getName());
if(tmpContact != null && !contact.getId().equals(tmpContact.getId()))
throw new RuntimeException("联系人的姓名相同,请重新输入");
this.entityManager.update(contact);
}
}
//=======================================================
//远程调用的案例
//=======================================================
/**
*
* @author <a href="mailto:zhangyouqun@gmail.com">cnoss (QQ:86895156)</a>
*
*/
@Path( { "/testCallRemote"})
public class TestRemoteResource {
@Inject
@RemoteReference//注入远程资源对象
private ContactResource service;
@Get
public Page<Contact> listContacts(int page, int size) {
return this.service.listContacts(page, size);
}
}
请大家直接从SVN中获取JRest4Guice工程即可(使用Maven) 真诚希望大家提出宝贵意见,联系方式:
声明:JavaEye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|
|
| 返回顶楼 | |
|
最后更新时间:2008-06-07
不知开发的效率如何?
|
|
| 返回顶楼 | |
|
最后更新时间:2008-06-07
|
|
| 返回顶楼 | |
|
最后更新时间:2008-06-07
支持事务,通过@Transactional注解声明事务的类型
支持JAAS,通过@RolesAllowed注解声明操作所需要的角色 支持分布式资源对象,实现业务逻辑的分布式部署 这几点非常不错,很诱人 |
|
| 返回顶楼 | |
|
最后更新时间:2008-06-12
|
|
| 返回顶楼 | |
|
最后更新时间:2008-06-12
我的Email:nieyunf@gmail.com
|
|
| 返回顶楼 | |
|
最后更新时间:2008-06-13
根据我使用Guice的经验,应该尽量使用Constructor injection,用field injection测试会非常困难。
你的annotation有些像JAX-RS,是否有支持JAX-RS的想法? |
|
| 返回顶楼 | |
|
最后更新时间:2008-06-13
yizhuo 写道 根据我使用Guice的经验,应该尽量使用Constructor injection,用field injection测试会非常困难。
你的annotation有些像JAX-RS,是否有支持JAX-RS的想法?
引用 Guice best practices Field vs. method vs. constructor injection Field injection * + Most compact syntax (good for a trivial custom provider, e.g.) * - Can't take any special action upon injection * - Your class is not testable! Method injection * + Isn't field injection * + Only thing that works for some strange edge cases Constructor injection * + Fields can be final! * + Injection cannot possibly have been skipped, even if Guice is not in the picture * + Easy to see dependencies at a glance * + It's what the idea of construction is all about * - No optional injections * - Useless when Guice can't do instantiation itself, e.g. a servlet * - Subclasses need to "know about" the injections needed by their superclasses * - Less convenient for tests that only "care about" one of the parameters 以上说明得很清楚,三种方式各有优缺点,我的理解是:对于非自定义提供者的使用构造器级注入,对于要带业务逻辑的使用方法级注入,而对于自定义提供者的采用属性级注入。我的案例中有以下的代码片段:
@Inject
@RemoteReference //注入远程资源对象的引用
private ContactResource service;
private BaseEntityManager<String, Contact> entityManager;
@Inject
private void init(EntityManager em) {
this.entityManager = new BaseEntityManager<String, Contact>(
Contact.class, em);
}
private ContactService service;
@Inject
public ContactResource(ContactService service){
this.service = service;
}
|
|
| 返回顶楼 | |
|
最后更新时间:2008-06-13
最新变动 !!!
本次变动采纳了 agapple 写道 小建议一下, 能否像webwork一样,使用maven进行管理依赖
的建议,谢谢agapple提出的宝贵意见。
这样checkout就不需要下载额外的jar |
|
| 返回顶楼 | |
|
最后更新时间:2008-06-18
注解,又是注解。
改动注解就要重新编译。 已经没有了配置文件的有点,所以也就是说和硬编码没有什么区别。 动态语言代码就是配置文件~但是Java不行。 |
|
| 返回顶楼 | |




