Spring 3, REST using Jersey 2.6 and Grizzly integration example

In this tutorial I will show you how to integrate REST using Jersey 2.6, Spring 3 and Grizzly web server. So I will use here Grizzly web server so we don’t need to use any other external webserver. I had been googling for many times but I did not get any satisfactory example on REST using Jersey 2.6 with Spring and Grizzly web server. You will get many search results if you search for a REST example using Jersey but you will get most of them are using 1.8. You will also get few results for more than 2.6 like here https://jersey.java.net/documentation/latest/user-guide.html but if you use more than 2.6 then you have to use at least jdk 1.7 but my requirement was to use jdk version 1.6 only. So I decided to write a tutorial on this. So here we go.
Prerequisites

JDK 1.6
Spring 3.x
Jersey 2.6
Junit 4.11
Eclipse 3.6

List of jar files shown in below image

rest jersey 2.6 grizzly spring 3 integration

Create a Dynamic Web Project in Eclipse.

Put the jar files in the lib directory.

We will now look at the source codes.

Spring Service

 

Spring bean configuration in applicationContext.xml. Put this applicationContext.xml file under src folder.

 

Now create the domain object

 

Create resource class for REST service. Note that how we have injected Spring HelloService in this class.

 

Configure the deployment descriptor file to use Jersey for REST API. You will see the applicationContext.xml file is also configured here in the web.xml. This applicationContext.xml file is used for Spring beans configuration.

 

Now we need to test the service. So I have already said that we don’t need to use any external web server here. So we will see here how we can start and stop the Grizzly server and call the REST service in the java code. Below is the junit test class.

 

Now run the above class and see the output.

Output:

 

Those who want to use maven based web application please have a look at the below pom.xml

 

Please feel free to leave a comment if you have any query.

Soumitra

Software Professional, I am passionate to work on web/enterprise application. For more information please go to about me. You can follow on Twitter. You can be a friend on Facebook or Google Plus or Linkedin

Leave a Reply

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

Time limit is exhausted. Please reload CAPTCHA.