Enterprise Java

Integrating Jersey with Spring

Spring provides a lot of benefits and promotes best practices with its dependency injection mechanism, application lifecycle management and Hibernate support (just to mention some). In addition when you want to have a clean server-side REST-like JSON Api, I found Jersey to be quite handy. This article briefly highlights how both of them can be integrated.

In my little spare time I’m currently trying to revive one of my earlier private projects which never exited the private beta (I’ll write more about it once I publish it). The project consists of a JavaScript rich client interface with a Java server “back-end” hosted on Google AppEngine. I’m currently completely rewriting it and so I started out cleanly by creating a Jersey REST Api on the server-side, that exposes it’s data in JSON. An example of such Jersey-exposed class is the following:

@Path("/sourcecodeitems")
public class SourceCodeItemGateway {	
	...
	
	@GET
	@Produces(MediaType.APPLICATION_JSON)
	public List<sourcecodeitemdto> index(){
		ArrayList<sourcecodeitemdto> listOfItems = new ArrayList<sourcecodeitemdto>();
		
		for (SourceCodeItem item : sourceCodeItems) {
			listOfItems.add(new SourceCodeItemDTO(item));
		}
		
		return listOfItems;
	}
	
	...	
}

The according web.config looks as follows:

<web-app version="2.5" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 <servlet>
  <servlet-name>Jersey Web Application</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>
   <param-value>com.jsdev.myproject.service</param-value>
  </init-param>
  <init-param>
   <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
   <param-value>true</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
  <servlet-name>Jersey Web Application</servlet-name>
  <url-pattern>/backend/*</url-pattern>
 </servlet-mapping>
 <welcome-file-list>
  <welcome-file>welcome.jsp</welcome-file>
 </welcome-file-list>
</web-app>

Line 7 indicates the package where your Jersey resources lie and line 10 activates the auto-mapping feature of your POJOs to Json.

Integrating with Spring

In order to integrate Jersey with Spring, you first need to include the jersey-spring-<version>.jar that comes with the Jersey package download. Include it in your build-path. You can then either configure your Jersey resource (SourceCodeItemGateway above) using Spring annotations (@Component) or to do it xml-based like…

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

 <bean class="com.jsdev.mydevbook.service.SourceCodeItemGateway" name="sourceCodeItemGateway">
  <property name="pingService" ref="pingService">
 </property></bean>
 
 <bean class="com.jsdev.myproject.service.PingService" id="pingService">
 </bean>

</beans>

Line 3 shows the bean configuration of the Jersey resource class as well as a configured dependency (PingService) which will be managed and injected by Spring. Finally, you need to adapt the web.config file to properly hook in Spring with Jersey:

<servlet>
 <servlet-name>jersey-servlet</servlet-name>
 <servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
 <init-param>
  <param-name>com.sun.jersey.config.property.packages</param-name>
  <param-value>com.jsdev.myproject.service</param-value>
 </init-param>
 <init-param>
  <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
  <param-value>true</param-value>
 </init-param>
 <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
 <servlet-name>jersey-servlet</servlet-name>
 <url-pattern>/backend/*</url-pattern>
</servlet-mapping>

<context-param>
 <param-name>contextConfigLocation</param-name>
 <param-value>
  /WEB-INF/spring-service.xml
  /WEB-INF/spring-data.xml
 </param-value>
</context-param>

Note in line 3 how we instantiate the Jersey SpringServlet. Line 23 and 24 show the path to the Spring configuration files. The previously shown bean configuration is an excerpt from the spring-service.xml. spring-data.xml is supposed to contain everything related to the data access.

Reference: Integrating Jersey with Spring from our JCG partner Juri Strumpflohner at the Juri Strumpflohner’s TechBlog.

Related Articles :

Juri Strumpflohner

Juri Strumpflohner mainly operates in the web sector developing rich applications with HTML5 and JavaScript. Beside having a Java background and developing Android applications he currently works as a software architect in the e-government sector. When he’s not coding or blogging about his newest discoveries, he is practicing Yoseikan Budo where he owns a 2nd DAN.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
kalpesh soni
kalpesh soni
8 years ago

would you be able to give side by side example?

what would the code look like if there is no jersey-spring but pure jersey code initializing spring beans?
vs what the above article shows?

Mayank jain
Mayank jain
5 years ago

hello Sir ,

i didn’t understand that why we change in web.xml.

Change Jersey servlet from “com.sun.jersey.spi.container.servlet.ServletContainer” to “com.sun.jersey.spi.spring.container.servlet.SpringServlet“.

if we will not change then what happen and please explain what are uses of them.

Thanks

Back to top button