About 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.

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 :

Related Whitepaper:

Introduction to Web Applications Development

Kick start your web apps development with this introductory ebook!

This 376 page eBook 'Introduction to Web Applications Development', starts with an introduction to the internet, including a brief history of the TCT/IP protocol and World Wide Web. It defines the basic concepts for web servers and studies the case of Apache, the most used webserver, while other free software webservers are not forgotten. It continues with webpage design focusing on HTML and JavaScript. XML Schemas, their validation and transformation are covered as well as dynamic webpages built with CGI, PHP or JSP and database access.

Get it Now!  

Leave a Reply


× eight = 48



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books