为什么使用 JAX-RS/Jersey?

对不起,这个问题听起来很傻,但是在使用 Jersey 开发了我的一些 RESTful 服务之后,我问自己这个问题——如果 REST 仅仅是一个架构,而不是像 SOAP 那样的协议,为什么我们需要像 JAX-RS 那样的规范?

事实上,我在谷歌上搜索了一些问题,比如“基于 HTTP 的 servlet 和 RESTful 服务之间的区别是什么”,为了总结社区的回答,我得到了:

  1. RESTful 服务开发(在 Jersey 上)是一种体系结构,它本质上使用 servlet。
  2. 与 JAX-RS 兼容的工具,如 Jersey,提供了对 XML/JSON 数据的简单编组-解组,对开发人员有所帮助。
  3. REST 帮助我们以一种比普通 servlet 高效得多的方式使用 GET/POST/PUT/DELETE。

根据这些答案,我猜如果我编写一个使用 JAXB (用于处理自动序列化)的 servlet,并且在 servlet 代码中有效地使用 GET/POST/PUT/DELETE,我就不会使用 Jersey 这样的工具,因此也不会使用 JAX-RS。

我知道我传达这个声明是非常错误的,请纠正我。

PS: 当我不得不用 PHP 开发一些 RESTful 服务时,我确实产生了这个疑问。在浏览了一些 RESTful PHP 代码之后,我意识到它们只是相同的旧 PHP 脚本,带有一些处理 XML/JSON 的助手方法。

54281 次浏览

REST is an architecture, which inherently uses servlets.

No, it is not. REST is an architecture style which can be implemented using servlets, but does not inherently use them, nor inherently have anything to do with Java.

JAX-RS is a JSR Specification defining a Java API for RESTful Web Services.

Jersey is a specific implementation of JAX-RS.

As to whether to use Jersey or try to be compliant to the JAX-RS specification, that's sort of up to you. If it makes your work easier, great! If not no one's forcing you.

Why use JAX-RS / Jersey?

Short Answer

Because it makes the development of RESTful services easier.

Long Answer

JAX-RS is a standard that makes it easy to create a RESTful service that can be deployed to any Java application server: GlassFish, WebLogic, WebSphere, JBoss, etc.

JAX-RS is part of Java EE, and when JAX-RS is used with other Java EE technologies it becomes even easier to create your RESTful service:

  • EJB - A session bean is used as the service implementation and also handles the transaction semantics.
  • JAX-RS - Used to expose the session bean as a RESTful service
  • JPA - Used to persist the POJOs to the database. Note how the EntityManager is injected onto the session bean.
  • JAXB - Used to convert the POJO to/from XML (in GlassFish it can also be used to convert the POJO to/from JSON). JAX-RS by default handles the interaction with the JAXB implementation.

Sample JAX-RS Service

package org.example;


import java.util.List;


import javax.ejb.*;
import javax.persistence.*;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;


@Stateless
@LocalBean
@Path("/customers")
public class CustomerService {


@PersistenceContext(unitName="CustomerService",
type=PersistenceContextType.TRANSACTION)
EntityManager entityManager;


@POST
@Consumes(MediaType.APPLICATION_XML)
public void create(Customer customer) {
entityManager.persist(customer);
}


@GET
@Produces(MediaType.APPLICATION_XML)
@Path("{id}")
public Customer read(@PathParam("id") long id) {
return entityManager.find(Customer.class, id);
}


@PUT
@Consumes(MediaType.APPLICATION_XML)
public void update(Customer customer) {
entityManager.merge(customer);
}


@DELETE
@Path("{id}")
public void delete(@PathParam("id") long id) {
Customer customer = read(id);
if(null != customer) {
entityManager.remove(customer);
}
}


@GET
@Produces(MediaType.APPLICATION_XML)
@Path("findCustomersByCity/{city}")
public List<Customer> findCustomersByCity(@PathParam("city") String city) {
Query query = entityManager.createNamedQuery("findCustomersByCity");
query.setParameter("city", city);
return query.getResultList();
}


}

For More Information: