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 :

4 comments:

oakleyses said...

louis vuitton handbags, oakley sunglasses, louboutin, longchamp outlet, nike shoes, louis vuitton outlet stores, chanel handbags, burberry outlet, prada outlet, jordan shoes, tiffany and co, michael kors outlet, tory burch outlet, louis vuitton outlet, longchamp handbags, nike free, true religion jeans, michael kors outlet, kate spade outlet, polo ralph lauren outlet, tiffany and co, prada handbags, polo ralph lauren outlet, michael kors outlet, michael kors outlet, longchamp handbags, oakley sunglasses, ray ban sunglasses, kate spade handbags, burberry outlet, louis vuitton outlet, louboutin outlet, louboutin, coach factory outlet, air max, air max, coach outlet, gucci outlet, christian louboutin shoes, michael kors outlet, coach purses, ray ban sunglasses, michael kors outlet, louis vuitton, coach outlet store online, true religion jeans, oakley sunglasses cheap

oakleyses said...

ralph lauren, lululemon, air max, hollister, north face, nike air max, polo lacoste, vanessa bruno, timberland, vans pas cher, louboutin, louis vuitton, oakley pas cher, air max pas cher, nike roshe run, air max, true religion outlet, barbour, sac longchamp, air force, hollister, sac louis vuitton, nike free, polo ralph lauren, nike trainers, louis vuitton uk, nike roshe, sac hermes, longchamp, michael kors, sac burberry, sac guess, mulberry, new balance pas cher, converse pas cher, sac louis vuitton, hogan outlet, nike tn, north face, true religion outlet, ray ban pas cher, michael kors, air jordan, nike blazer, nike free pas cher, michael kors pas cher, abercrombie and fitch, ray ban sunglasses

oakleyses said...

mac cosmetics, mont blanc, marc jacobs, canada goose outlet, nike huarache, vans shoes, soccer jerseys, hollister, giuseppe zanotti, beats by dre, abercrombie and fitch, longchamp, insanity workout, celine handbags, bottega veneta, ghd, nfl jerseys, north face outlet, chi flat iron, ugg boots, birkin bag, ugg australia, canada goose, herve leger, ugg pas cher, rolex watches, valentino shoes, canada goose uk, canada goose, ferragamo shoes, canada goose, ugg boots, uggs outlet, north face jackets, soccer shoes, asics running shoes, new balance shoes, p90x, lululemon outlet, canada goose jackets, mcm handbags, instyler, babyliss pro, ugg, wedding dresses, jimmy choo outlet, reebok outlet, nike roshe run

oakleyses said...

parajumpers, karen millen, air max, converse, pandora charms, moncler, louboutin, moncler, links of london, lancel, juicy couture outlet, oakley, hollister, pandora charms, supra shoes, thomas sabo, canada goose, gucci, wedding dresses, timberland boots, swarovski crystal, air max, coach outlet store online, moncler, ray ban, canada goose, moncler, ugg, louis vuitton, swarovski, hollister, montre homme, moncler, hollister clothing store, ralph lauren, rolex watches, moncler outlet, moncler, iphone 6 cases, baseball bats, juicy couture outlet, toms shoes, vans, pandora jewelry, ugg, converse shoes