JAX-RS Restful WebService using Jersey

JAX-RS Restful WebService using Jersey

learninjava
Aug 06, 2015 - Webservices
 

WebService 

Any service which follows the REST (REpresentational State Transfer) architectural pattern is said to be restful. Let us look at an example on how to create a simple restful webservice using Jersey and Spring. The following software has been used for this tutorial :
1. Apache Maven 3.0.4
2. Spring 3.2.6
3. Jersey 1.19
4. Jetty 6.1.10/Tomcat 7
First, we need to create a basic Jersey web project structure.
Create a basic maven web project structure similar to below (Refer Download Source) :
jersey-webservice-project-structure
 Remember!
You MUST use the Test case file name pattern as below because the maven surefire plugin expects the test file names in the following patterns :
**/Test*.java
**/*Test.java
**/*TestCase.java
For your convenience, we have provided configurations for running the webservice on both Jetty and Tomcat servers.
First, let us take a look at tomcat configuration. Later we will see about jetty configuration. There is a cool feature in jetty that attracts many developers. we will see what is it later in this tutorial.
 

Using Tomcat : 

1. Change the default pom.xml as below :

loading...
You will notice that there are 4 important plugins in the pom.xml out of which 2 are optional. We will see each of them in detail :
maven-surefire-plugin - [Optional] This plugin is added to specify the service.url in the pom.xml itself. We will see its usage while running the web service client.
tomcat7-maven-plugin - This is the tomcat plugin where we will deploy our angry world webservice.
maven-compiler-plugin - This plugin specifies the Java compiler source and target versions.
maven-eclipse-plugin - [Optional] This plugin is used to generate the .classpath and .project files which enables us to use this project in eclipse.
The other important sections includes dependencies and properties which are used to specify the jar file dependencies and properties respectively. We have commented out a certain section of dependencies. We will see that when we create a client, Restful client using Jersey

2. HelloAngryWorld.java :

loading...
This is the actual webservice source file, so lets take a look at it.
This webservice exposes two methods one of type plain text and other of type JsonBean. These methods uses some important annotations:
@Path - annotation at class level specifies the context path to access this webservice.
@Path - annotation at method level specifies the path to access the actual method in the webservice.
@PathParam - annotation is used to accept input from the request URL path specified by @Path.
@Produces - annotation is used to specify the return type of the method.
@Consumes - annotation is used to specify the return type accepted as input to a method.
For a complete listing of annotations refer : jax-rs-annotations

3. JsonBean.java :

loading...

4. beans.xml :

loading...
PreferencesPlaceholderConfigurer and ServletContextPropertyPlaceholderConfigurer are used to handle properties files. See references section at the bottom of this tutorial for more information.
The most significant part of beans.xml is the bean definition of the webservice we are going to expose.

5. web.xml :

loading...
The configurations in web.xml are straighforward. SpringServlet is the actual servlet that reads the xml configurations and makes the service beans as webservices and handles the requests and responses to the exposed services.
 Remember!
Observe that SpringServlet is spring enabled servlet for exposing webservices using Jersey. If we dont want to use Jersey then you can use ServletContainer instead (disabled in web.xml).
ContextLoaderListener is the ROOT web application context that is used by spring.
com.sun.jersey.config.property.packages - specifies the package name for the webservice artifacts
POJOMappingFeature - is responsible for mapping java objects to JSON.
Once the above files are ready, type the below command to start the tomcat container :
mvn clean package -DskipTests=true tomcat7:run
This command cleans the target directory, compiles, builds war and deploys it to tomcat. Notice that we used skipTests = true switch. This is to make sure that the test case are not run before the server starts.
Navigate to the URL :
http://localhost:9090/jaxrs-service/helloAngryWorld/echo/tweet
Output:

tweet

This is an echo service so whatever you input in the URL should be echoed to your browser.
 

Using Jetty : 

1. pom.xml :

The pom.xml is pretty much similar to the one we have used for tomcat configuration except that the tomcat plugin is replaced with the jetty equivalent. Also the port number is changed 8080 just to distinguish between tomcat and jetty.
loading...
All other steps are same as above.
To start the jetty container and the webservice use the below command :
mvn clean package -DskipTests=true jetty:run
Navigate to the URL :
http://localhost:8080/jaxrs-service/helloAngryWorld/echo/tweet
Output:

tweet

 

Advantage of using Jetty - Hot code deployment : 

By default, Jetty scans the following directories :
target/classes - for changes to Java sources and
src/main/webapp - for changes to web sources.
If any source file is modified, the changes are autodeployed to the jetty container without manually restarting for the changes to take effect.
To test this hot code deployment, try to change any source file. We have changed the Path param of ping method from echo to ping. Now, recompiled the source using mvn compile command.
The moment the source file is compiled, notice the server console. You will observe that jetty hot deploys the code automatically and you can access the changes via browser instantly.
Navigate to the URL :
http://localhost:8080/jaxrs-service/helloAngryWorld/ping/tweet
Output:

tweet

Thats all folks !! Happy coding. If you feel this helped you, keep supporting us by   or  or  below or on the articles on social media.
 
Like us on: