Overview

In this blog, we will cover

  • What is JAX-RS
  • JAX-RS Specification
  • Implementation provider for JAX-RS
  • Why follow JAX-RS

What is JAX-RS

  • JAX-RS: Java API for RESTful Web Services (JAX-RS) is a Java programming language API spec that provides support in creating web services according to the Representational State Transfer (REST) architectural pattern.
  • JAX-RS 2.0 was released in May 2013.
  • JAX-RS 2.1 was released in Sep 2017.

JAX-RS Specification

  • JAX-RS provides some annotations to aid in mapping a resource class (a POJO) as a web resource.
  • The annotations use the Java package javax.ws.rs.
  • Following are the Annotations provided
@Path => specifies the relative path for a resource class or method.
@GET, @PUT, @POST, @DELETE and @HEAD => specify the HTTP request type of a resource.
@Produces => specifies the response Internet media types (used for content negotiation).
@Consumes => specifies the accepted request Internet media types.


@PathParam => binds the method parameter to a path segment.
@QueryParam => binds the method parameter to the value of an HTTP query parameter.
@MatrixParam => binds the method parameter to the value of an HTTP matrix parameter.
@HeaderParam => binds the method parameter to an HTTP header value.
@CookieParam => binds the method parameter to a cookie value.
@FormParam => binds the method parameter to a form value.
@DefaultValue => specifies a default value for the above bindings when the key is not found.
@Context => returns the entire context of the object (for example @Context HttpServletRequest request).

Implementation provider for JAX-RS

  • Apache CXF, an open-source Web service framework
  • Jersey, the reference implementation from Oracle
  • RESTeasy, JBoss’s implementation
  • and many more…

Why follow JAX-RS

  • I will show you a piece of code that expose REST Api for JBoss, GlassFish, and Web Server.
  • As stated above, JAX-RS is specification or in short, it defines some standard/rules that make it easy to create a RESTful service that can be deployed to any Java application server like Glassfish, Jboss, Apache Tomee.
  • Let’s say you developed an Application without using JAX-RS standard and happily running it in Jboss.
  • Tomorrow you have a new requirement to move your application to GlassFish.
  • In this case, you will have to rewrite your REST APIs logic again.
  • Now consider the same scenario and assume you would have written your REST API using RESTEasy jar (JAX-RS implementation for Jboss).
  • Now when you shift to GlassFish all you have to do is change implementation jar (Jersey JAX-RS implementation for Glassfish).
  • Here is Sample Code
For JBoss
// pom.xml

<!-- RESTEasy - JAX-RS implementation for Jboss -->
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>3.0.13.Final</version>
<scope>provided</scope>
</dependency>


// Rest API

import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Consumes;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@Path("/test")
public interface TestIf {

@POST
@Consumes(MediaType.APPLICATION_XML)
@Produces(MediaType.APPLICATION_XML)
public Response addEmployee( Employee e, @PathParam("id") int id, @QueryParam("fromDate")String fromDate ) throws URISyntaxException
{
// logic
return Response.created(new URI("/rest/employees/"+e.getId())).build();
}
}
For Glassfish
// pom.xml


<!-- Jersey - JAX-RS implementation for Glassfish -->
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>${jersey2.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>${jersey2.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>${jersey2.version}</version>
</dependency>

// Rest API

import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Consumes;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@Path("/test")
public interface TestIf {

@POST
@Consumes(MediaType.APPLICATION_XML)
@Produces(MediaType.APPLICATION_XML)
public Response addEmployee( Employee e, @PathParam("id") int id, @QueryParam("fromDate")String fromDate ) throws URISyntaxException
{
// logic
return Response.created(new URI("/rest/employees/"+e.getId())).build();
}
For Web Application (Jetty)
// pom.xml

<!-- Apache CFX - JAX-RS implementation for Web Application like Jetty, Apache Tomee -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
<version>3.1.7</version>
</dependency>

<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>3.1.7</version>
</dependency>



// Code remains same as in Glashfish or Jboss
  • See pom.xml for all three blocks.
  • You will see different jars for JAX-RS implementation but there is no change in Code.
  • That’s the benefit of following a specification. eg: JPA

 


0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *