【发布时间】:2012-06-26 19:16:11
【问题描述】:
我通过 Jersey in Java (JAX-RS) 开发了一个宁静的网络服务:http://www.vogella.com/articles/REST/article.html
然后我使用 Hibernate 技术将数据映射到数据库。
最后我开发了一个安卓应用来显示数据。
这是我的 Web 服务中的一个方法示例:
@GET
@Path("/project_id/username/get/{projectId}/{username}/")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response deliverableList(@PathParam("projectId") long projectId,
@PathParam("username") String username) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
List<Deliverable> list = null;
try {
list= (List<Deliverable>) session.createQuery(
"from Deliverable as d where d.project.id= :id").setLong("id", projectId).list();
} catch (HibernateException e) {
e.printStackTrace();
session.getTransaction().rollback();
}
session.getTransaction().commit();
return Response.status(201).entity(list).build();
}
如您所见,我使用“Response.status(201).entity(list).build()”来传输数据列表。这是一个好方法吗?如果不是,您对传输数据有什么建议。请用一些代码和例子来支持你的解释。
【问题讨论】: