Spring 4 + RESTEasy 3 + Jackson JSON Integration Example with Tomcat

By Arvind Rai, March 14, 2015
On this page we learn Spring 4, RESTEasy 3 and Jackson JSON integration with Tomcat. RESTEasy framework provides org.jboss.resteasy.plugins.spring.SpringContextLoaderListener to integrate spring. Our RESTEasy web service class will be annotated with @Component annotation, so that we can inject spring bean. To get JSON response RESTEasy uses Jackson provider. In our example, we are creating a RESTEasy and spring demo project in which we will create an Employee service with will respond employee detail for the given employee id.

Software Required to Run Example

To run the example, we are using following software.
1. Java 7
2. Tomcat 8
3. Gradle
4. Eclipse

Project Structure in Eclipse

Find the project structure in eclipse which we are using in our example.
Spring 4 + RESTEasy 3 + Jackson JSON Integration Example with Tomcat

Gradle File for Spring Boot, RESTEasy JAR and Jackson Provider

Find the Gradle file to resolve dependencies of Spring, RESTEasy and Jackson provider.
build.gradle
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'war'
archivesBaseName = 'resteasyservice'
version = '1' 
repositories {
        mavenCentral()
}
dependencies {
	compile 'org.jboss.resteasy:resteasy-jaxrs:3.0.10.Final'
	compile 'org.jboss.resteasy:resteasy-spring:3.0.10.Final'
	compile 'org.springframework.boot:spring-boot-starter-web:1.2.2.RELEASE'
	compile 'org.jboss.resteasy:resteasy-jackson2-provider:3.0.10.Final'
}  

web.xml: Declare SpringContextLoaderListener after ResteasyBootstrap

RESTEasy provides SpringContextLoaderListener to integrate Spring. In web.xml, we need to declare SpringContextLoaderListener after ResteasyBootstrap.

resteasy.servlet.mapping.prefix: Context parameter to assign prefix for URL mapping used by RESTEasy.
contextConfigLocation: Context parameter used by spring to find XML file.
org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap: ResteasyBootstrap initializes ServletContext.
org.jboss.resteasy.plugins.spring.SpringContextLoaderListener: RESTEasy provides SpringContextLoaderListener for spring integration which must be declared after ResteasyBootstrap because SpringContextLoaderListener uses ServletContext initialized by ResteasyBootstrap.
org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher: Dispatcher used by RESTEasy.

Find the web.xml file.
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <context-param>
        <param-name>resteasy.servlet.mapping.prefix</param-name>
        <param-value>/employee</param-value>
    </context-param>
	<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
    </listener>
    <listener>
        <listener-class>org.jboss.resteasy.plugins.spring.SpringContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>RESTEasyService</servlet-name>
        <servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
        <init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>com.concretepage.EmployeeApplication</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>RESTEasyService</servlet-name>
        <url-pattern>/employee/*</url-pattern>
    </servlet-mapping>
</web-app> 

Create Spring Bean

In our project we are creating a class to fetch employee. Find the employee repository interface.
EmployeeRepository.java
package com.concretepage;
import java.util.Map;
public interface EmployeeRepository {
     Map<String,String> getEmpDetail(String id);
} 
Find the repository implementation class annotated with @Service.
EmployeeRepositoryImpl.java
package com.concretepage;
import java.util.HashMap;
import java.util.Map;
import org.springframework.stereotype.Service;
@Service
public class EmployeeRepositoryImpl implements EmployeeRepository {
    @Override	
    public Map<String,String> getEmpDetail(String id){
	   	 Map<String,String> map = new HashMap<String,String>();
	   	 map.put("Id", id);
	         map.put("Name", "Arvind Rai");
	   	 map.put("Location", "Varanasi");
	   	 return map;
    }
} 
We need to annotate our repository class with spring @Service annotation, so that it can be considered as spring bean to be injected.

Spring XML

Find the spring XML.
dispatcher-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    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">
       <context:component-scan base-package="com.concretepage" />
</beans> 

Create Web Service Class

We are creating a web service to fetch employee and for that we are creating a JAX-RS method. To get return type as JSON, we need to provide content type as application/json . RESTEasy will use Jackson provider to return JSON response. To be considered as Spring component, we need to annotate our web service class with spring @Component annotation. And in this way, we will be able to inject our spring service or bean in RESTEasy service class.
EmployeeService.java
package com.concretepage;
import java.util.Map;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Path("/manage" )
@Component
public class EmployeeService {
    @Autowired
    private EmployeeRepository employeeRepository;
	
    @GET
    @Path("/{id}")
    @Produces("application/json")
    public Response getEmp(@PathParam("id") String id) {
    	Map<String,String> map = employeeRepository.getEmpDetail(id);
        return Response.ok(map).build();
    }
} 
Find the JAX-RS Application Class.
EmployeeApplication.java
package com.concretepage;
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.core.Application;
public class EmployeeApplication extends Application {
    private Set<Object> singletons = new HashSet<Object>();
    public EmployeeApplication() {
        singletons.add(new EmployeeService());
    }
    @Override
    public Set<Object> getSingletons() {
        return singletons;
    }
} 

Output

Run Gradle file to create WAR and deploy it into Tomcat 8 and access the URL as
http://localhost:8080/resteasyservice-1/employee/manage/222
And we will get JSON response as
{"Name":"Arvind Rai","Location":"Varanasi","Id":"222"} 
Find the print screen.
Spring 4 + RESTEasy 3 + Jackson JSON Integration Example with Tomcat

Download Complete Source Code

POSTED BY
ARVIND RAI
ARVIND RAI
LEARN MORE








©2024 concretepage.com | Privacy Policy | Contact Us