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.
- Create New Maven Project : File -> New -> Maven Project
- Select Simple archetype and give the below details
- pom.xml file: Click here to download pom
- 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:
- create applicationContext.xml file in src/main/resources folder as which would availe to classpath always:
-
<?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>
-
- create web.xml with below contents:
-
<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>
-
- UserServiceImpl class is: Services need to annotate with @Service and add @Transactional as to make this service as transactional aware.
- Rest Resource Controller is: Annotate this with @Component Spring annotation and you can inject spring beans like userService with @Autowired.
- Now deploy the application in tomcat
- Enter the url in browser: http://localhost:8080/jersey-spring-tutorial/rest/user/user-info/1
- Response:
-
{"status":200,"message":"User info", "user":{"firstName":"javasavvy","lastName":"exmp", "email":"[email protected]","sex":"M", "password":null,"userId":1}}
-
- Response:
Hope this helps
Click here to download the code
In the Next Tutorial, We will see Jersey Spring 4 Hibernate Maven tutorial
5 thoughts on “Jersey Spring integration”
Comments are closed.
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.
Thanks Hrishi
Good Job, thanks
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