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 -> https://jersey.java.net
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
Following jar must be in classpath
- commons-logging-1.1.3.jar
- hk2-api-2.4.0-b06.jar
- hk2-locator-2.4.0-b06.jar
- hk2-utils-2.4.0-b06.jar
- jackson-annotations-2.4.6.jar
- jackson-core-2.4.6.jar
- jackson-databind-2.4.6.jar
- jackson-jaxrs-base-2.3.2.jar
- jackson-jaxrs-json-provider-2.3.2.jar
- jackson-module-jaxb-annotations-2.3.2.jar
- javax.annotation-api-1.2.jar
- javax.inject-2.4.0-b25.jar
- javax.ws.rs-api-2.0.1.jar
- jersey-client.jar
- jersey-common.jar
- jersey-container-servlet-core.jar
- jersey-container-servlet.jar
- jersey-guava-2.19.jar
- jersey-media-json-jackson-2.14.jar
- jersey-server.jar
- jersey-spring3-2.14.jar
- spring-aop-4.2.0.RELEASE.jar
- spring-beans-4.2.0.RELEASE.jar
- spring-bridge-2.4.0-b06.jar
- spring-context-4.2.0.RELEASE.jar
- spring-core-4.2.0.RELEASE.jar
- spring-expression-4.2.0.RELEASE.jar
- spring-web-4.2.0.RELEASE.jar
- 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 a Student Object
public class Student {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Create a Service Interface
This service interface will defines methods which will be works as spring service
public interface StudentService {
String helloStudent(String name);
}
Implement the Service Interface
Here we implement the service interface created on the previous step
public class StudentServiceImpl implements StudentService{
@Override
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)
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 com.entity.Student;
@Path("Services")
public class StudentController {
@Autowired
private StudentService studentService;
@POST
@Path("/changeName")
@Produces(MediaType.APPLICATION_JSON)
public Student changeName(Student student) {
student.setName("HELLO " + student.getName());
return student;
}
@GET
@Path("/getName")
@Produces(MediaType.APPLICATION_JSON)
public Student getName() {
Student student = new Student();
student.setName("Rockey");
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
Create ApplicationConfig
Here we register the StudentController class, so that jersey will invoke properly
import org.glassfish.jersey.server.ResourceConfig;
import com.controller.StudentController;
public class ApplicationConfig extends ResourceConfig {
public ApplicationConfig() {
register(StudentController.class);
}
}
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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd" > <bean id="studentService" class="com.service.StudentServiceImpl"/> </beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>Jersey2Example</display-name> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>WEB-INF/applicationContext.xml</param-value> </context-param> <servlet> <servlet-name>jersey</servlet-name> <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> <init-param> <param-name>javax.ws.rs.Application</param-name> <param-value>com.config.ApplicationConfig</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>jersey</servlet-name> <url-pattern>/rest/*</url-pattern> </servlet-mapping> </web-app>