Why use JAX-RS / Jersey?

Sorry, this question sounds silly, but after developing some of my RESTful services using Jersey, I asked myself the question: if REST is just an architecture and not a protocol like SOAP, why do we need a specification like JAX -RS?

I actually googled for questions such as "What is the difference between servlets and RESTful services over HTTP" and to summarize the community responses, I got:

  • Developing a RESTful service (in Jersey) is an architecture that essentially uses servlets.
  • JAX-RS-compatible tools, such as Jersey, provide easy sorting / unmarshalling of XML / JSON data, helping developers.
  • REST helps us use GET / POST / PUT / DELETE in a way that is more efficient than regular servlets.

According to these answers, I assume that if I write a servlet that uses JAXB (to work with automatic serialization) and I use GET / POST / PUT / DELETE effectively in my servlet code, I don’t use a tool like Jersey , and therefore JAX-RS.

I know that I am terribly mistaken in accepting this expression, please correct me.

PS: This doubt really happened when I had to develop some RESTful services in PHP. After going through some of the RESTful PHP codes, I realized that they are the same old PHP scripts, and some helper methods for processing XML / JSON.

+76
rest jersey jax-rs
Aug 13 '11 at 17:34
source share
2 answers

Why use JAX-RS / Jersey?

Short answer

Because it facilitates the development of RESTful services.

Long answer

JAX-RS is a standard that simplifies the creation of 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, creating a RESTful service is even easier:

  • EJB - The session bean is used as a service implementation and also handles transaction semantics.
  • JAX-RS - used to represent a session bean as a RESTful service
  • JPA - used to save POJO in the database. Notice how the EntityManager is embedded in the session bean.
  • JAXB - used to convert POJO to / from XML (in GlassFish it can also be used to convert POJO to / from JSON). JAX-RS, by default, handles interactions with the JAXB implementation.

JAX-RS Service Example

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 further information:

+67
Aug 14 2018-11-12T00:
source share

REST is an architecture that essentially uses servlets.

No, it is not. REST is an architectural style that can be implemented using servlets, but does not use them by nature and has nothing to do with Java.

JAX-RS is a JSR specification that defines the Java API for RESTful web services.

Jersey is a specific implementation of JAX-RS.

As for using a jersey or trying to comply with the JAX-RS specification, it is up to you. If it makes your job easier, great! If nobody forces you.

+53
Aug 13 2018-11-11T00:
source share



All Articles