Jersey Spring Integration

Jersey spring integration tutorial is all about integrating jersey with spring. Most of the spring applications requires REST Web Services  at back end for mobile/UI, so  they may use Spring MVC framework/Jersey to generated REST web services. In addition, Jersey applications requires Service & data base layer, hence spring come in picture to manage dependencies.

So, How to integrate jersey with spring?  further to this tutorial, below are prerequisite tutorials:

Installations:

  • Eclipse Mars
  • Spring 4
  • Jersey 1.19
  • Maven 4

Spring Jersey Tutorial:

  • Jersey requires only jersey-server jar, but for spring requires jersey-spring dependency jar
  •  Add com.sun.jersey.spi.spring.container.servlet.SpringServlet in web.xml that com.sun.jersey.spi.container.servlet.ServletContainer.
  • Load  applicationContext  in web.xml as a context loader
  • Annotate Rest resource class with @Component to mark as Spring Bean.

 

  1. Create New Maven Project : File -> New -> Maven Project
  2. Select Simple archetype and give the below detailsJersey Spring Details
  3. pom.xml file: Click here to download pom
  4. Create Project structure like below:
    • org.javasavvy.rest.controller : For REST Resource Controllers and create UserRestResource.java
    • org.javasavvy.tutorial.services : Service Layer
    • org.javasavvy.tutorial.dao : DAO layer
    • org.javasavvy.tutorial.entity : Database JPA entities
    • org.javasavvy.rest.modal :   REST POJO classes
    • The project Structure will look like:
      Jersey Spring Maven Tutorial
  5. create applicationContext.xml file in src/main/resources folder as which would availe to classpath always:
    1. <?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:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
       
       <context:annotation-config />
       <context:component-scan base-package="org.javasavvy.rest.controller" />
       <context:component-scan base-package="org.javasavvy.tutorial.services" /> 
       <context:component-scan base-package="org.javasavvy.tutorial.dao" /> 
       
      </beans>
  6. create web.xml with below contents:
    1. <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
       http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
       version="2.5">
              <welcome-file-list>
                  <welcome-file>index.html</welcome-file>
                  <welcome-file>index.jsp</welcome-file>
              </welcome-file-list>
              <context-param>
                  <param-name>webAppRootKey</param-name>
                  <param-value>webapp.root.one</param-value>
               </context-param>
              <context-param>
                  <param-name>log4jConfigLocation</param-name>
                  <param-value>/WEB-INF/log4j.properties</param-value>
              </context-param>
             <listener>
                  <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
            </listener>
            <listener>
                 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
             </listener>
         <!-- <listener>
               <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
              </listener> -->
       
            <context-param>
              <param-name>contextConfigLocation</param-name>
             <param-value>
               classpath:applicationContext.xml
              </param-value>
            </context-param>
      
           <servlet>
              <servlet-name>jersey-spring</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>org.javasavvy.rest</param-value>
               </init-param>
             <init-param>
                   <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
                   <param-value>true</param-value>
              </init-param>
             <load-on-startup>1</load-on-startup>
          </servlet>
      
           <servlet-mapping>
                <servlet-name>jersey-spring</servlet-name>
                <url-pattern>/rest/*</url-pattern>
            </servlet-mapping>
      </web-app>
  7. UserServiceImpl class is:  Services need to annotate with @Service and add @Transactional as to make this service as transactional aware.Spring @Service
  8. Rest Resource Controller is: Annotate this with @Component Spring annotation and you can inject spring beans like userService with @Autowired.Jersey Spring controller
  9. Now deploy the application in tomcat
  10. Enter the url in browser: http://localhost:8080/jersey-spring-tutorial/rest/user/user-info/1
    • Response:
      1. {"status":200,"message":"User info",
         "user":{"firstName":"javasavvy","lastName":"exmp",
         "email":"jayaram@gmail.com","sex":"M",
         "password":null,"userId":1}}

Hope this helps

Click here to download the code

 

In the Next Tutorial, We will see Jersey Spring 4 Hibernate Maven tutorial

5 Comments

  1. Hrishikesh

    Hi

    Thank you very much the topic you explained is so clear and gave me a good idea on this.
    I appreciate your work and thought of sharing every minute details with all.

    Reply
    1. Jayaram Pokuri (Post author)

      Thanks Hrishi

      Reply
  2. Pingback: Jersey Spring Hibernate Example,jersey spring jpa example

  3. AntuanSoft

    Good Job, thanks

    Reply
  4. mayan jain

    hello Sir ,

    i didn’t understand that why we change 2 things in web.xml.

    1) Register Spring “ContextLoaderListener” listener class.
    2.) Change Jersey servlet from “com.sun.jersey.spi.container.servlet.ServletContainer” to “com.sun.jersey.spi.spring.container.servlet.SpringServlet“.

    if we will not change these two thing then what happen and please explain what are uses of them.

    Thanks

    Reply

Leave a Comment

Your email address will not be published. Required fields are marked *