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.
The according web.config looks as follows:
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…
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:
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 :