Community Pick: Many members of our community have endorsed this article.

Spring Jersey Integration

     I've been learning Spring and Jersey for the past few months and to say in simple, i am pretty much impressed with these frameworks. Many developers feel it awkward to implement a RESTful Web Services with such a popular Web Application Frameworks Spring,Struts etc. I am going to explain you how to integrate a simple REST based Web-Service with Spring 3.

There are 5 steps involved in this.
1. Add Jersey Container in web.xml and the url pattern to be mapped.
2. Copy the needed JAR file.
3. Create a Resource class.
4. Add reference in spring-beans.xml
5. Access the service

NOTE : All the codes posted here are tested and works great.

1. Adding Jersey Container in web.xml
<?xml version="1.0" encoding="UTF-8"?>
                      <web-app xmlns:xsi=""
                      	xmlns="" xmlns:web=""
                      	id="WebApp_ID" version="2.5">
                            <!-- Helps locate the applicationContext.xml file -->
                      	<!-- Configuration for Jersey Web Services starts here -->
                                               <!-- The package in which the Resource classes located-->
                                     <!-- Enables us to access the WS by pointing your browser to this URL : http://localhost:8080/any_name/{service-path}-->
                      	<!-- Configuration for Jersey Web Services ends here -->

Open in new window

2.Copy the required JAR file

Download the Jersey JAR files from the
Paste it to the WEB-INF/lib folder of your project

3. Create a Resource class
package org.pathto.jersey.resource;
                      @Path("hello") // The path by which we access the service
                      public class ServicesImpl implemets Services
                        @GET // The HTTP method by which we access the service
                        @Produces("application/html") // The output MIME Type
                        public String getMethod()
                           return "<b>Hello World, Welcome to Jersey + Spring Integrattion</b>";

Open in new window

Basically in Spring, we use interface to create a Service. This is same in case of Web Services too. I here simple wrote the ServicesImpl which means, there must be a interface with the implemented methods.

4. Add reference in spring-beans.xml

Open you spring-context.xml (or) applicationContext.xml (or) prj_name-servlet.xml
Add the below code snippet
<bean id="wservices" class="org.pathto.jersey.resource.ServicesImpl" />

Open in new window

This will just point the Resource class to the Spring to identify this as Web Service Resource

5. Access the service

To access the HelloWorld Service, open the Web Browser and type http://localhost:8080/any_name/hello.

The browser just display the message saying
Hello World, Welcome to Jersey + Spring Integrattion.

Simple isn't it.

Reasons why i choose Jersey over other implementations.

1. Jersey is sun's reference implementation of JSR-311, which means lots of active community so it assures us with a lot of documentation and ofcourse help too.

2. Everything is POJO. Whether its Resource or whatever format, everything is in clear POJO code which means cleaner implementation.

3. Getting XML,JSON input and parsing them is not a tedious process like in other frameworks. Generating XMLs is a just a matter of minute. Less, cleaner, straight-forward codes.

4. Easy integration with Web Application frameworks . Lets say for example Spring,Struts etc.

Hope you guys got a bunch of reason to go with Jersey and know how to integrate it with Spring. Please feel free to ask ny questions regarding that.

Comments (1)


Thanks for such a warm welcome for me. I always like to help people by solving their technology related problems and i myself as a programmer, would love to explain a concept as simple as i can. I hope i'll publish much more articles tp help others understand.

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.