Tuesday, July 03, 2012

Integrate Jersey and Spring

This post describes a way to integrate spring with Jersey resource classes. It expands on the earlier sample Restful Web Services With Jersey API. The code here has been implemented on following configuration
  • Tomcat 7
  • Java 7
  • Spring 3.1.1
  • Jersey 1.12
To show the use of Spring, I added MyService class which will be injected into RestWS class. For this example, we start out with the sample code in Restful Web Services With Jersey API and make the following changes...
  1. Web.xml: In the Web Deployment Descriptor, the Jersey Servlet has to be modified to use com.sun.jersey.spi.spring.container.servlet.SpringServlet instead of com.sun.jersey.spi.container.servlet.ServletContainer
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app 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_3_0.xsd"
        id="WebApp_ID" version="3.0">
        <display-name>JerseyRest</display-name>
        <welcome-file-list>
            <welcome-file>index.html</welcome-file>
        </welcome-file-list>
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/applicationContext.xml</param-value>
        </context-param>
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
    
        <servlet>
            <servlet-name>Jersey Web Application</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.blogspot.aoj.restws</param-value>
            </init-param>
    
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>Jersey Web Application</servlet-name>
            <url-pattern>/rest/*</url-pattern>
        </servlet-mapping>
    </web-app>
  2. RestWS: There is not much change in the RestWS.java file, other than adding the field MyService which will be injected by spring. The following sample code shows @Autowire, which works with @Component annotation for spring to inject dependencies. However, you can implement the same example without Autowiring by simply creating a bean in spring context file.
    /**
     * @author Abhi Vuyyuru
     */
    
    package com.blogspot.aoj.restws;
    
    import javax.ws.rs.GET;
    import javax.ws.rs.Path;
    import javax.ws.rs.PathParam;
    import javax.ws.rs.Produces;
    import javax.ws.rs.core.MediaType;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    
    import com.blogspot.aoj.service.MyService;
    
    @Path("/restws")
    @Component
    public class RestWS {
        
        @Autowired
        private MyService myService;
    
        @GET
        @Path("concat/i/{i}/j/{j}")
        @Produces(MediaType.TEXT_HTML)
        public String concat(@PathParam("i") String i, @PathParam("j") String j) {
            return myService.concat(i, j);
        }
    
        public MyService getMyService() {
            return myService;
        }
    
        public void setMyService(MyService myService) {
            this.myService = myService;
        }
    
    }
  3. /WEB-INF/applicationContext.xml: The following code shows code for Autowire and otherwise. Simply comment the unused version
    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"     xmlns:context="http://www.springframework.org/schema/context"     xsi:schemaLocation="             http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">         <bean id="myService" class="com.blogspot.aoj.service.MyService" />     <bean id="restWS" class="com.blogspot.aoj.restws.RestWS">         <property name="myService" ref="myService"></property>     </bean>     <!-- <context:component-scan base-package="com.blogspot.aoj.restws" /> --> </beans>
  4. Client will be the same as used in the previous code sample Restful Web Services With Jersey API
  5. The Jersey Spring Servlet is part of the jersey-spring.jar file which can be downloaded from the Jersey download site
  6. Finally, the following is the ant build file that I used.
    <project name="jerseyRest" default="compile" basedir=".">
        <property file="build.properties" />
        <property file="${user.home}/build.properties" />
        <property name="app.name" value="jerseyRest" />
        <property name="app.path" value="/${app.name}" />
        <property name="app.version" value="0.1-dev" />
        <property name="build.home" value="${basedir}/build" />
        <property name="catalina.home" value="c:/tomcat7" />
        <property name="dist.home" value="${basedir}/dist" />
        <property name="docs.home" value="${basedir}/docs" />
        <property name="manager.url" value="http://localhost:8080/manager/text" />
        <property name="manager.username" value="tomcat" />
        <property name="manager.password" value="tomcat" />
        <property name="src.home" value="${basedir}/src" />
        <property name="web.home" value="${basedir}/WebContent" />
        <path id="compile.classpath">
            <fileset dir="${catalina.home}/bin">
                <include name="*.jar" />
            </fileset>
            <pathelement location="${catalina.home}/lib" />
            <fileset dir="${catalina.home}/lib">
                <include name="*.jar" />
            </fileset>
            <fileset dir="${web.home}/WEB-INF/lib">
                <include name="*.jar" />
            </fileset>
    
        </path>
        <taskdef resource="org/apache/catalina/ant/catalina.tasks" classpathref="compile.classpath" />
        <taskdef name="deploy" classname="org.apache.catalina.ant.DeployTask" classpathref="compile.classpath" />
        <taskdef name="list" classname="org.apache.catalina.ant.ListTask" classpathref="compile.classpath" />
        <taskdef name="reload" classname="org.apache.catalina.ant.ReloadTask" classpathref="compile.classpath" />
        <taskdef name="findleaks" classname="org.apache.catalina.ant.FindLeaksTask" classpathref="compile.classpath" />
        <taskdef name="resources" classname="org.apache.catalina.ant.ResourcesTask" classpathref="compile.classpath" />
        <taskdef name="start" classname="org.apache.catalina.ant.StartTask" classpathref="compile.classpath" />
        <taskdef name="stop" classname="org.apache.catalina.ant.StopTask" classpathref="compile.classpath" />
        <taskdef name="undeploy" classname="org.apache.catalina.ant.UndeployTask" classpathref="compile.classpath" />
    
        <property name="compile.debug" value="true" />
        <property name="compile.deprecation" value="false" />
        <property name="compile.optimize" value="true" />
    
    
        <target name="all" depends="clean,compile" description="Clean build and dist directories, then compile" />
    
        <target name="clean" description="Delete old build and dist directories">
            <delete dir="${build.home}" />
            <delete dir="${dist.home}" />
        </target>
    
    
        <target name="compile" depends="prepare" description="Compile Java sources">
            <mkdir dir="${build.home}/WEB-INF/classes" />
            <javac srcdir="${src.home}" destdir="${build.home}/WEB-INF/classes" debug="${compile.debug}" deprecation="${compile.deprecation}" optimize="${compile.optimize}">
                <classpath refid="compile.classpath" />
            </javac>
            <copy todir="${build.home}/WEB-INF/classes">
                <fileset dir="${src.home}" excludes="**/*.java" />
            </copy>
        </target>
    
        <target name="dist" depends="compile,javadoc" description="Create binary distribution">
            <mkdir dir="${dist.home}/docs" />
            <copy todir="${dist.home}/docs">
                <fileset dir="${docs.home}" />
            </copy>
            <jar jarfile="${dist.home}/${app.name}-${app.version}.war" basedir="${build.home}" />
        </target>
    
    
        <target name="install" depends="compile" description="Install application to servlet container">
            <deploy url="${manager.url}" username="${manager.username}" password="${manager.password}" path="${app.path}" localWar="file://${build.home}" />
        </target>
    
    
        <target name="javadoc" depends="compile" description="Create Javadoc API documentation">
            <mkdir dir="${dist.home}/docs/api" />
            <javadoc sourcepath="${src.home}" destdir="${dist.home}/docs/api" packagenames="*">
                <classpath refid="compile.classpath" />
            </javadoc>
        </target>
    
    
    
        <target name="list" description="List installed applications on servlet container">
            <list url="${manager.url}" username="${manager.username}" password="${manager.password}" />
        </target>
    
    
        <target name="prepare">
            <mkdir dir="${build.home}" />
            <mkdir dir="${build.home}/WEB-INF" />
            <mkdir dir="${build.home}/WEB-INF/classes" />
            <copy todir="${build.home}">
                <fileset dir="${web.home}" />
            </copy>
            <mkdir dir="${build.home}/WEB-INF/lib" />
        </target>
    
        <target name="reload" depends="compile" description="Reload application on servlet container">
            <reload url="${manager.url}" username="${manager.username}" password="${manager.password}" path="${app.path}" />
        </target>
        <target name="remove" description="Remove application on servlet container">
            <undeploy url="${manager.url}" username="${manager.username}" password="${manager.password}" path="${app.path}" />
        </target>
    </project>

6 comments:

  1. Nice article ... and nice blog! Keep going!

    ---
    devmain

    ReplyDelete
  2. Hey, I had a great time reading your website. Would you please consider adding a link to my website on your page. Please email me back.

    Regards,

    Angela
    angelabrooks741 gmail.com

    ReplyDelete
  3. Sterling Training Center providing excellent in-class and online training.
    http://sterlingtrainingcenter.com

    ReplyDelete
  4. Your means of describing the whole thing in this piece of writing
    is truly good, every one can without difficulty be aware of it,
    Thanks a lot. 먹튀검증

    ReplyDelete

Popular Posts