Jersey 2 Spring Integration Example

Jersey 2 Spring Integration Example explains step by step details of Creating / Developing Java rest Web services using Jersey, Spring and Eclipse

JAX-RS is Java API for RESTful Webservices which is very rely upon Representational State Transfer model, you can view JAX-RS specification 

JAX-RS uses annotations for simplifying the development efforts.

Jersey framework is more than the JAX-RS Reference Implementation. Jersey provides it’s own API that extend the JAX-RS toolkit with additional features and utilities to further simplify RESTful service and client development. Jersey also exposes numerous extension SPIs so that developers may extend Jersey to best suit their needs

Reference ->

You can see the below example, which is demonstrating How to create a Restful service using Jersey 2 with Spring framework

Required Libraries

You need to download

  1. JDK 7
  2. Eclipse 4.2
  3. Jersey 2.19
  4. Tomcat 8
  5. Spring 4.2

Following jar must be in classpath

  1. commons-logging-1.1.3.jar
  2. hk2-api-2.4.0-b06.jar
  3. hk2-locator-2.4.0-b06.jar
  4. hk2-utils-2.4.0-b06.jar
  5. jackson-annotations-2.4.6.jar
  6. jackson-core-2.4.6.jar
  7. jackson-databind-2.4.6.jar
  8. jackson-jaxrs-base-2.3.2.jar
  9. jackson-jaxrs-json-provider-2.3.2.jar
  10. jackson-module-jaxb-annotations-2.3.2.jar
  11. javax.annotation-api-1.2.jar
  12. javax.inject-2.4.0-b25.jar
  14. jersey-client.jar
  15. jersey-common.jar
  16. jersey-container-servlet-core.jar
  17. jersey-container-servlet.jar
  18. jersey-guava-2.19.jar
  19. jersey-media-json-jackson-2.14.jar
  20. jersey-server.jar
  21. jersey-spring3-2.14.jar
  22. spring-aop-4.2.0.RELEASE.jar
  23. spring-beans-4.2.0.RELEASE.jar
  24. spring-bridge-2.4.0-b06.jar
  25. spring-context-4.2.0.RELEASE.jar
  26. spring-core-4.2.0.RELEASE.jar
  27. spring-expression-4.2.0.RELEASE.jar
  28. spring-web-4.2.0.RELEASE.jar
  29. validation-api-1.1.0.Final.jar

Jersey 2 Spring Example

I am creating a sample restful service project that pass Student object and return with some changes on that object. The service is using simple POJO (Plain Old Java Object) bean.

Firstly create a Dynamic Web Project (File->New->Dynamic Web Project) named "Jersey2SpringExample" according to following screenshot

Create Jersey Project Jersey Jersey2Spring Tutorial

Create a Student Object

package com.entity;

public class Student {
private String name;

public String getName() {
return name;

public void setName(String name) { = name;


Create a Service Interface

This service interface will defines methods which will be works as spring service

package com.service;

public interface StudentService {

String helloStudent(String name);


Implement the Service Interface

Here we implement the service interface created on the previous step

package com.service;

public class StudentServiceImpl implements StudentService{

public String helloStudent(String name) {
return "Hello "+name;


Create a Controller

Here StudentController will defines which methods of restful service, to be invoked by the client

Here we are using one example showing with GET method & another with POST method

GET---> Calling this method will not result any changes to the server

POST---> Calling this method will result changes to the server, This have more secure than GET method

@Autowire---> annotation inject the spring service inside the controller. (Also see the appplicationContext.xml, where we created the studentService bean)

package com.controller;


import com.entity.Student;

public class StudentController {

private StudentService studentService;

public Student changeName(Student student) {
student.setName("HELLO " + student.getName());
return student;

public Student getName() {
Student student = new Student();
return student;

@Consumes annotation specifies, the request is coming from the client

you can specify the Mime type as @Consumes("application/xml"), if the request is in xml format

@Produces annotation specifies, the response is going to the client

you can specify the Mime type as @Produces ("application/xml"), if the response need to be in xml format

1 2 Next

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