For the past few days, i have scratched my head finding a good tutorial for Spring + Jersey integration and ebd up with nothing. So finally decided to do it on my own, and for surprise it is much easier than i expected. The simple thing is it relies with the web.xml. Nothing else. I give you a sample web.xml which i used for my project
<?xml version=”1.0″ encoding=”UTF-8″?>
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” id=”WebApp_ID” version=”2.5″>
<display-name>xxx</display-name>
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>xxx</param-value>
</context-param>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log4j.properties</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext-hibernate.xml,/WEB-INF/xxx-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>xxx1</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>xxx.yyy.zzz.service</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>xxx1</servlet-name>
<url-pattern>/resource/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>xxx</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>xxx</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>10</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<error-page>
<exception-type>java.lang.Exception</exception-type>
/WEB-INF/jsp/uncaughtException.jsp
</error-page>
</web-app>
Look at the servlet-name’s. We only need to differentiate the service by servlet-name but still it can be used locally by the project itself.
Simple isn’t it.
Why use Jersey?
- Reference Implementation JSR-311 and so lot of documentation, means lot of help from community.
- Clean POJO with awesome annotations.
For ex :
@Path(“/customers”)
@Singleton
@Consumes(“application/xml”)
@Produces(“application/xml”)
Simple and now you know what all these will do.
3. Easy integration with other frameworks.
now you guys got a reason to go with Jersey. a’ight.