Dear reader of Juri's TechBlog,
I moved my blog to a new domain and a new hosting solution as well. I'm now blogging on juristr.com.

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;
	}
	
	...	
}
Actually this is quite similar (just a bit more complicated ;)) to an ASP.net MVC controller as described here. The according web.config looks as follows:
<?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"
	version="2.5">
	<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 12 indicates the package where your Jersey resources lie and line 15 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...
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	     http://www.springframework.org/schema/beans/spring-beans.xsd">


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

</beans>
Line 8 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 4 how we instantiate the Jersey SpringServlet. Line 25 and 26 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.

Posts you might also be interested in..

Credits: Hoctro | Jack Book

3 Comments:

Anonymous said...

Can u please tell what version of spring and jersey you were using?

Juri Strumpflohner said...

It should be jersey-core-1.8 and spring-2.5.6 (an older version actually if I'm well informed)

Anonymous said...

Can you made a example for me:). Thanks a lot.

Post a Comment