Sunday, February 2, 2014

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 Jerseyto 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:
01@Path("/sourcecodeitems")
02public class SourceCodeItemGateway {   
03    ...
04     
05    @GET
06    @Produces(MediaType.APPLICATION_JSON)
07    public List index(){
08        ArrayList listOfItems = new ArrayList();
09         
10        for (SourceCodeItem item : sourceCodeItems) {
11            listOfItems.add(new SourceCodeItemDTO(item));
12        }
13         
14        return listOfItems;
15    }
16     
17    ...
18}
The according web.config looks as follows:
02 <servlet>
03  <servlet-name>Jersey Web Application</servlet-name>
04  <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
05  <init-param>
06   <param-name>com.sun.jersey.config.property.packages</param-name>
07   <param-value>com.jsdev.myproject.service</param-value>
08  </init-param>
09  <init-param>
10   <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
11   <param-value>true</param-value>
12  </init-param>
13  <load-on-startup>1</load-on-startup>
14 </servlet>
15 <servlet-mapping>
16  <servlet-name>Jersey Web Application</servlet-name>
17  <url-pattern>/backend/*</url-pattern>
18 </servlet-mapping>
19 <welcome-file-list>
20  <welcome-file>welcome.jsp</welcome-file>
21 </welcome-file-list>
22</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-.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…
02 
03 <bean class="com.jsdev.mydevbook.service.SourceCodeItemGateway" name="sourceCodeItemGateway">
04  <property name="pingService" ref="pingService">
05 </property></bean>
06  
07 <bean class="com.jsdev.myproject.service.PingService" id="pingService">
08 </bean>
09 
10</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:
01<servlet>
02 <servlet-name>jersey-servlet</servlet-name>
03 <servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
04 <init-param>
05  <param-name>com.sun.jersey.config.property.packages</param-name>
06  <param-value>com.jsdev.myproject.service</param-value>
07 </init-param>
08 <init-param>
09  <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
10  <param-value>true</param-value>
11 </init-param>
12 <load-on-startup>1</load-on-startup>
13</servlet>
14 
15<servlet-mapping>
16 <servlet-name>jersey-servlet</servlet-name>
17 <url-pattern>/backend/*</url-pattern>
18</servlet-mapping>
19 
20<context-param>
21 <param-name>contextConfigLocation</param-name>
22 <param-value>
23  /WEB-INF/spring-service.xml
24  /WEB-INF/spring-data.xml
25 </param-value>
26</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 :

No comments: