Spring Jersey Integration

AID: 2908
  • Status: Published

6050 points

  • Byrajkumar_pb
  • TypeTutorial
  • Posted on2010-04-21 at 02:22:52
Awards
  • Community Pick
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>
                                    
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:

Select allOpen 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>";
  }
}
                                    
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:

Select allOpen 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" />
                                    
1:

Select allOpen 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.
Asked On
2010-04-21 at 02:22:52ID2908
Tags

Spring 3

,

Jersey

Topic

Web Frameworks

Views
9704

Comments

Author Comment

by: rajkumar_pb on 2010-04-21 at 06:54:48ID: 13627

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.

Add your Comment

Please Sign up or Log in to comment on this article.

Join Experts Exchange Today

Gain Access to all our Tech Resources

Get personalized answers

Ask unlimited questions

Access Proven Solutions

Search 3.2 million solutions

Read In-Depth How-To Guides

1000+ articles, demos, & tips

Watch Step by Step Tutorials

Learn direct from top tech pros

And Much More!

Your complete tech resource

See Plans and Pricing

30-day free trial. Register in 60 seconds.

Loading Advertisement...

Top Web Frameworks Experts

  1. Ray_Paseur

    43,268

    0 points yesterday

    Profile
    Rank: Savant
  2. smadeira

    11,000

    0 points yesterday

    Profile
    Rank: Wizard
  3. stephencolson

    6,000

    0 points yesterday

    Profile
    Rank: Master
  4. ChrisStanyon

    5,000

    0 points yesterday

    Profile
    Rank: Sage
  5. DaveBaldwin

    4,600

    0 points yesterday

    Profile
    Rank: Genius
  6. tbsgadi

    4,300

    0 points yesterday

    Profile
    Rank: Genius
  7. rowby

    4,000

    0 points yesterday

    Profile
    Rank: Master
  8. frisoft

    4,000

    0 points yesterday

    Profile
  9. maeltar

    4,000

    0 points yesterday

    Profile
    Rank: Guru
  10. Arrow_1

    3,800

    0 points yesterday

    Profile
    Rank: Master
  11. Slick812

    3,200

    0 points yesterday

    Profile
    Rank: Sage
  12. bportlock

    3,200

    0 points yesterday

    Profile
    Rank: Genius
  13. TheLearnedOne

    2,800

    0 points yesterday

    Profile
    Rank: Savant
  14. mplungjan

    2,800

    0 points yesterday

    Profile
    Rank: Savant
  15. pepr

    2,800

    0 points yesterday

    Profile
    Rank: Genius
  16. harperse

    2,800

    0 points yesterday

    Profile
    Rank: Guru
  17. ingwa

    2,800

    0 points yesterday

    Profile
    Rank: Sage
  18. tagit

    2,800

    0 points yesterday

    Profile
    Rank: Genius
  19. johanntagle

    2,800

    0 points yesterday

    Profile
    Rank: Sage
  20. Derokorian

    2,800

    0 points yesterday

    Profile
    Rank: Guru
  21. webmatrixpune

    2,600

    0 points yesterday

    Profile
    Rank: Guru
  22. bigeven2002

    2,300

    0 points yesterday

    Profile
    Rank: Guru
  23. arnold

    2,000

    0 points yesterday

    Profile
    Rank: Genius
  24. xterm

    2,000

    0 points yesterday

    Profile
    Rank: Sage
  25. Masteraco

    2,000

    0 points yesterday

    Profile
    Rank: Wizard

Hall Of Fame