Home  >  Spring 4

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.
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'war'
archivesBaseName = 'resteasyservice'
version = '1' 
repositories {
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.
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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"

Create Spring Bean

In our project we are creating a class to fetch employee. Find the employee repository interface.
package com.concretepage;
import java.util.Map;
public interface EmployeeRepository {
     Map<String,String> getEmpDetail(String id);
Find the repository implementation class annotated with @Service.
package com.concretepage;
import java.util.HashMap;
import java.util.Map;
import org.springframework.stereotype.Service;
public class EmployeeRepositoryImpl implements EmployeeRepository {
    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.
<beans xmlns="http://www.springframework.org/schema/beans"
       <context:component-scan base-package="com.concretepage" />

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.
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" )
public class EmployeeService {
    private EmployeeRepository employeeRepository;
    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.
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());
    public Set<Object> getSingletons() {
        return singletons;


Run Gradle file to create WAR and deploy it into Tomcat 8 and access the URL as
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


©2022 concretepage.com | Privacy Policy | Contact Us