How to Integrate Jersey with Spring Example in Java Development?

Aegis Java developers are sharing this tutorial with global Java development community to make them learn how to integrate Jersey rest-ws with spring 3.x. The java experts are also going to use Eclipse IDE and Maven in development process. Read further to get the complete story.

We are coming up with this tutorial to teach you how to integrate Jersey rest-ws with spring 3.x

Here are the list of technology we are going to use in this tutorial.

1) Eclipse IDE

2) Maven

3) Jersey

4) Spring

 

1) Create Maven Project

  1. File >> New >> Other >> Maven Project.
  2. Check ON Create a simple project (Skip archetype selection).
  3. fill up the input fields group id, version.
  4. artifact id (it will be the name of the project).
  5. packaging should be WAR in our case, and Finish.

 You have now a blank skeleton of maven project.

 

2) Add dependencies to pom.xml

We will start with adding required dependencies to pom.xml for jersey and spring.

 

            <!-- Declared version properties. ->

                <properties>

                                <spring.version>3.2.5.RELEASE</spring.version>

                </properties>

 

                <dependencies>

                                <!-- Jersey -->

                                <dependency>

                                                <groupId>com.sun.jersey</groupId>

                                                <artifactId>jersey-server</artifactId>

                                                <version>1.8</version>

                                </dependency>

 

                                <!-- Jersey + JSON -->

                                <dependency>

                                                <groupId>com.sun.jersey</groupId>

                                                <artifactId>jersey-json</artifactId>

                                                <version>1.8</version>

                                </dependency>

 

                                <!-- Jersey + Spring (jersey-spring will load spring dependencies by default, if we want to use specific spring version just exclude the default dependencies) -->

                                <dependency>

                                                <groupId>com.sun.jersey.contribs</groupId>

                                                <artifactId>jersey-spring</artifactId>

                                                <version>1.8</version>

                                                <exclusions>

                                                                <exclusion>

                                                                                <groupId>org.springframework</groupId>

                                                                                <artifactId>spring</artifactId>

                                                                </exclusion>

                                                                <exclusion>

                                                                                <groupId>org.springframework</groupId>

                                                                                <artifactId>spring-core</artifactId>

                                                                </exclusion>

                                                                <exclusion>

                                                                                <groupId>org.springframework</groupId>

                                                                                <artifactId>spring-aop</artifactId>

                                                                </exclusion>

                                                                <exclusion>

                                                                                <groupId>org.springframework</groupId>

                                                                                <artifactId>spring-web</artifactId>

                                                                </exclusion>

                                                                <exclusion>

                                                                                <groupId>org.springframework</groupId>

                                                                                <artifactId>spring-beans</artifactId>

                                                                </exclusion>

                                                                <exclusion>

                                                                                <groupId>org.springframework</groupId>

                                                                                <artifactId>spring-context</artifactId>

                                                                </exclusion>

                                                </exclusions>

                                </dependency>

 

                                <!-- Spring 3 dependencies : Manually add spring dependencies here we have used spring 3.2.5.RELEASE version -->

                                <dependency>

                                                <groupId>org.springframework</groupId>

                                                <artifactId>spring-core</artifactId>

                                                <version>${spring.version}</version>

                                </dependency>

 

                                <dependency>

                                                <groupId>org.springframework</groupId>

                                                <artifactId>spring-context</artifactId>

                                                <version>${spring.version}</version>

                                </dependency>

 

                                <dependency>

                                                <groupId>org.springframework</groupId>

                                                <artifactId>spring-web</artifactId>

                                                <version>${spring.version}</version>

                                </dependency>

 

                                <!-- Logging -->

                                <dependency>

                                                <groupId>log4j</groupId>

                                                <artifactId>log4j</artifactId>

                                                <version>1.2.14</version>

                                </dependency>

 

                                <dependency>

                                                <groupId>org.slf4j</groupId>

                                                <artifactId>slf4j-api</artifactId>

                                                <version>1.6.1</version>

                                </dependency>

 

                                <dependency>

                                                <groupId>org.slf4j</groupId>

                                                <artifactId>jcl-over-slf4j</artifactId>

                                                <version>1.6.0</version>

                                </dependency>

 

                                <dependency>

                                                <groupId>org.slf4j</groupId>

                                                <artifactId>slf4j-log4j12</artifactId>

                                                <version>1.6.0</version>

                                </dependency>

                </dependencies>

 

3) Create a Business Layer first

As we have all the dependencies added lets start with Business layer, A simple employeeService bean will be registered in spring configuration and later we can inject its implementation class to use the services.

 

AegisEmployeeService.java

package com.aegis.ws.rest.services;

 

import java.util.List;

import com.aegis.ws.model.Employee;

 

public interface AegisEmployeeService {

 

                public List<Employee> getAllEmployees();

 

                public Employee getEmployeeById(Long empId);

}

 

 

AegisEmployeeServiceImpl.java

package com.aegis.ws.rest.services.impl;

 

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.aegis.ws.model.Employee;

import com.aegis.ws.rest.services.AegisEmployeeService;

 

public class AegisEmployeeServiceImpl implements EmployeeService {

 

                public String getAllEmployees() {

                                return “Aegis : List of Employee : Jersey With Spring Demo.”;

                }

 

                public Employee getEmployeeById(Long empId) {

                                Employee emp = new Employee();

                                emp.setEmpId(new Long(13));

                                emp.setFirstName("Chirag");

                                emp.setLastName("L");

                                return emp;

                }

}

 

4) Create applicationContext.xml

                Register beans with it and also enable component-scan feature that scans classpath to auto-register annotated components.

 

applicationContext.xml

<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"

                xsi:schemaLocation="http://www.springframework.org/schema/beans

                                                                                                http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

                                                                                                http://www.springframework.org/schema/context

                                                                                                http://www.springframework.org/schema/context/spring-context-3.0.xsd">

 

                <!-- scans the classpath for annotated components (including @Repostory

                                and @Service that will be auto-registered as Spring beans -->

                <context:component-scan base-package="com.aegis.ws.rest" />

 

                <bean id="aegisEmployeeService"                 class="com.aegis.ws.rest.services.impl.AegisEmployeeServiceImpl" />

 

</beans>

 

5) Jersey : Now its time to create Jersey REST methods.

 

EmployeeWSBean.java

 

package com.aegis.ws.rest;

 

import javax.ws.rs.Consumes;

import javax.ws.rs.GET;

import javax.ws.rs.POST;

import javax.ws.rs.Path;

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.aegis.ws.model.Employee;

import com.aegis.ws.rest.services.EmployeeService;

 

@Component

@Path("/employee")

public class EmployeeWSBean {

 

                @Autowired

                private AegisEmployeeService aegisEmployeeService;

 

                @GET

                @Path("/getEmployeeList")

                public String getEmployeeList() {

                                return employeeService.getAllEmployees();

                }

 

                @GET

                @Path("/getEmployeeTest")

                @Produces(MediaType.APPLICATION_JSON)

                public Employee getEmployee() {

                                Employee emp = new Employee();

                                emp.setEmpId(new Long(13));

                                emp.setFirstName("Chirag");

                                emp.setLastName("Loliyana");

                                return emp;

                }

 

                public void setAegisEmployeeService(AegisEmployeeService aegisEmployeeService) {

                                this. aegisEmployeeService = aegisEmployeeService;

                }

}

 

6) Lets come to the core part now, Configure WEB.xml for Jersey and Spring.

 

Location : src/main/webapp/WEB-INF/web.xml

 

web.xml

 

<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">

                <display-name>Jersey RestFul Demo</display-name>

 

                <context-param>

                                <param-name>contextConfigLocation</param-name>

                                <param-value>classpath:applicationContext.xml</param-value>

                </context-param>

 

                <listener>

                                <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

                </listener>

 

                <servlet>

                                <servlet-name>jersey-serlvet</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.aegis.ws.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-serlvet</servlet-name>

                                <url-pattern>/restws/*</url-pattern>

                </servlet-mapping>

 

</web-app>

 

Execute maven command clean install to generate WAR.

 

 

7) Run Project

Just right click on project from elipse and select Run AS >> Run on Server.

 

Open your browser and hit the below URL

 

http://localhost:8080/restful-jersey-spring-demo/restws/employee/getEmployeeTest

 

f:id:aegissofttech:20150416184329p:plain

We hope this article has meet your requirements and now you know the process of integration of Jersey with Spring. Aegis Java developers are here to answer your queries related to java development. Share your reviews and comment below. 

 You are done with the implementation.