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

Spring Jersey Integration

Published:
Hi,
     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="http://www.w3.org/2001/XMLSchema-instance"
                      	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
                      	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
                      	id="WebApp_ID" version="2.5">
                      	<display-name>prj_name</display-name>
                            <!-- Helps locate the applicationContext.xml file -->
                      	<context-param>
                      		<param-name>contextConfigLocation</param-name>
                      		<param-value>/WEB-INF/applicationContext-hibernate.xml,/WEB-INF/Erp-servlet.xml</param-value>
                      	</context-param>
                      	
                      	<listener>
                      		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
                      	</listener>
                      	
                      	<servlet>
                      		<servlet-name>any_name</servlet-name>
                      		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
                      		<load-on-startup>2</load-on-startup>
                      	</servlet>
                      	
                      	<servlet-mapping>
                      		<servlet-name>any_name</servlet-name>
                      		<url-pattern>*.do</url-pattern>
                      	</servlet-mapping>
                      	<!-- Configuration for Jersey Web Services starts here -->
                      	<servlet>
                      		<servlet-name>jersey</servlet-name>
                      		<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
                      		<init-param>
                      			<param-name>com.sun.jersey.config.property.packages</param-name>
                                               <!-- The package in which the Resource classes located-->
                      			<param-value>org.pathto.jersey.resource</param-value>
                      		</init-param>
                      		<load-on-startup>1</load-on-startup>
                      	</servlet>
                      	
                      	<servlet-mapping>
                      		<servlet-name>jersey</servlet-name>
                                     <!-- Enables us to access the WS by pointing your browser to this URL : http://localhost:8080/any_name/{service-path}-->
                      		<url-pattern>/*</url-pattern>
                      	</servlet-mapping>
                      	<!-- Configuration for Jersey Web Services ends here -->
                      	<session-config>
                      		<session-timeout>10</session-timeout>
                      	</session-config>
                      	
                      	<welcome-file-list>
                      		<welcome-file>index.jsp</welcome-file>
                      	</welcome-file-list>
                      </web-app>
                      

Open in new window


2.Copy the required JAR file

Download the Jersey JAR files from the https://jersey.dev.java.net/
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.
1
15,293 Views

Comments (1)

Author

Commented:
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.