This chapter provides readers with a brief introduction on JAX-RS 2.0. It also includes the principles required to be followed in RESTful web services and the new features that have been introduced in JAX-RS 2.0, such as client API, filters and interceptors, client-side and server-side asynchronous. It also contains some other prominent features, such as listing of modules and dependencies that are used for implementing JAX-RS 2.0. Besides the previously mentioned features, it also provides a demonstration of JAX-RS 2.0 with examples.
JAX-RS 2.0 is a framework that helps you in writing the RESTful web services on the client side as well as on the server side. Jersey 2.0 is the reference implementation of the JAX-RS 2.0 (JSR 339 specification). Along with the enhancements in Java EE 7, JAX-RS 2.0 has also been revised dramatically.
The following is a list of the RESTful principles that must be followed:
An ID should be assigned to everything
Things should be linked together
A common set of methods must be used
Multiple representations should be allowed
Stateless communication must be kept
Before moving ahead, let's look at the existing features of JAX-RS 1.0:
JAX-RS 2.0 remains consistent with the central theme of Java EE 7, but it contains other long-awaited APIs too. These APIs are mostly focused around what is referred to as Simplified API. They can be categorized as follows:
However, Jersey 2.0 can be deployed on several web containers that support Servlet 2.5 or higher, Grizzly 2 HTTP server (which is also the default server for testing), and OSGi containers. For the new async feature of JAX-RS 2.0, Server-Side Events (SSE), we need containers supporting Servlet 3.0.
As a part of Java EE 7, it is easy to create RESTful web services using JAX-RS 2.0. Using JAX-RS annotations, we can map the POJO resource class to the URIs and the URI templates. Using annotations, it becomes easier to develop web services. Different
@*Param
annotations are available to access the values of the user request:
@PathParam
@QueryParam
@MatrixParam
@HeaderParam
@CookieParam
@FormParam
@DefaultValue
@Context
JAX-RS also supports all the Java data types to be supplied in the @*Param
annotations. There are other APIs available to create the RESTful web services, but mostly they require more coding. However, in the Jersey implementation of JAX-RS, it is simpler and easier to create the RESTful web services. JAX-RS manages to encode and decode the request/response content according to the media type it mentioned.
To provide backward compatibility, all Jersey applications are compiled with Java SE 6. Thus, we can run our Jersey application on Java SE 6 easily though JAX-RS 2.0 is part of Java EE 7.
To create Jersey-based applications, we require several dependencies and for other modules, we require third-party dependencies. Jersey dependencies are loosely-coupled and separated according to the module dependencies. Jersey dependencies are lightweight, so our application has much complexity for dependencies and creation of the RESTful web services.
The following are the core Jersey modules:
Jersey Core
jersey-client
jersey-common
jersey-server
Note
You can download the Jersey Core JAX-RS 2.0 Bundle from:
http://repo1.maven.org/maven2/org/glassfish/jersey/bundles/jaxrs-ri/2.0/jaxrs-ri-2.0.zip
Apart from these dependencies, there are Jersey Containers, Connectors, Media, Extensions, Test Framework, and Glassfish Bundles. These dependencies can be easily plugged. You can find Jersey 2.0 binaries on the Jersey 2.0 Maven repository,
http://repo1.maven.org/maven2/org/glassfish/jersey/,
and on
https://maven.java.net/content/repositories/releases/org/glassfish/jersey/.
The first step for creating a new project is to accumulate the required tools:
Java JDK (Version 6 or higher)
Eclipse IDE (Juno)
Apache Tomcat Server (Version 7) or Glassfish Server (Version 4.0)
Jersey Framework
Firstly, we will create a web project using Eclipse IDE. Go to File | New | Others | Dynamic Web Project. Follow the steps, and after the project is created, add the following libraries into the classpath:
asm-all-repackaged-2.2.0-b14.jar
cglib-2.2.0-b14.jar
guava-14.0.1.jar
hk2-api-2.2.0-b14.jar
hk2-locator-2.2.0-b14.jar
hk2-utils-2.2.0-b14.jar
javax.annotation-api-1.2.jar
javax.inject-2.2.0-b14.jar
javax.ws.rs-api-2.0.jar
jersey-client-2.2.jar
jersey-common-2.2.jar
jersey-container-servlet-core-2.2.jar
jersey-server-2.2.jar
osgi-resource-locator-1.0.1.jar
validation-api-1.1.0.Final.jar
Now, we need to configure web.xml
to the bound Jersey Container with the resources packages:
........... <servlet> <servlet-name>simpleJerseyExample</servlet-name> <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> <init-param> <param-name>jersey.config.server.provider.packages</param-name> <param-value>com.demo</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>simpleJerseyExample</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping> ...........
A servlet container is treated as a controller to redirect the specified resource that is being called. jersey.config.server.provider.packages
maps the resources that are available in the com.demo
package. So, whenever any resource is being requested, ServletContainer
checks into the com.demo
package for the resource URI and serves the request accordingly.
The next step is to create the
Resource
class that contains the business logic:
package com.demo; import javax.ws.rs.Path; import javax.ws.rs.Get; import javax.ws.rs.core.MediaType; import javax.ws.rs.Produces; /** * helloWorld Root Resource */ @Path("helloWorld") public class HelloWorldResource{ @GET @PRODUCES(MediaType.TEXT_PLAIN) public String greet(){ return "Hello World!!!"; } }
Tip
Downloading the example code
You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
A JAX-RS resource is an annotated POJO, which provides the so-called resource methods that are able to handle the HTTP requests for the URI paths bound to the resource. In the previous code, "helloWorld"
is the resource URI:
@Path("helloWorld")
To run the application, create a WAR file and deploy it on the Apache Tomcat Server. Once the project is deployed on the Tomcat server, we are ready to consume the "helloWorld" web service. We can type the resource URL in the browser or we can use curl
:
$curl http://localhost:8080/projectName/services/helloWorld Hello World!!!
We have covered a brief introduction about the JAX-RS 2.0 and Jersey 2.0 features that have enriched the JAX-RS web services. We've analyzed a simple RESTful web service example that shows the basic implementation of the JAX-RS 2.0. In the next chapter, we will see how to use the Server API with different examples of complete sets of code to implement the Server-side web services.