Main Tutorials

RESTEasy + Spring integration example

Note
Not much comments on this official RESTEasy guide to integrate Spring, the documentation need to be improve, because it’s simply too “abstract” for others to understand (at least to me :)).

Here we show you two general ways to inject Spring bean into JBoss RESTEasy, below solutions should works on most of the web frameworks and JAX-RS implementations also.

  1. Use WebApplicationContextUtils + ServletContext.
  2. Create a class to implement ApplicationContextAware intrefaces, with singleton pattern is recommended.

In this tutorial, we are integrating RESTEasy 2.2.1.GA with Spring 3.0.5.RELEASE.

1. Project Dependencies

Not many dependencies, just declare RESTEasy and Spring core in your Maven pom.xml file.


	<repositories>
		<repository>
		  <id>JBoss repository</id>
		  <url>https://repository.jboss.org/nexus/content/groups/public-jboss/</url>
		</repository>
	</repositories>

	<dependencies>
	
		<!-- JBoss RESTEasy -->
		<dependency>
			<groupId>org.jboss.resteasy</groupId>
			<artifactId>resteasy-jaxrs</artifactId>
			<version>2.2.1.GA</version>
		</dependency>

		<!-- Spring 3 dependencies -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>3.0.5.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>3.0.5.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>3.0.5.RELEASE</version>
		</dependency>

		<!-- need for solution 1, if your container don't have this -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>servlet-api</artifactId>
			<version>2.4</version>
		</dependency>

	</dependencies>

2. Spring Bean

A simple Spring bean named “customerBo“, later you will inject this bean into RESTEasy service, via Spring.


package com.mkyong.customer;

public interface CustomerBo{
	
	String getMsg();
	
}

package com.mkyong.customer.impl;

import com.mkyong.customer.CustomerBo;

public class CustomerBoImpl implements CustomerBo {

	public String getMsg() {

		return "RESTEasy + Spring example";
		
	}

}

File : applicationContext.xml


<beans xmlns="http://www.springframework.org/schema/beans"
	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 ">

	<bean id="customerBo" class="com.mkyong.customer.impl.CustomerBoImpl">
        </bean>

</beans>

3.1 WebApplicationContextUtils + ServletContext

The first solution is uses JAX-RS @Context to get the ServletContext, and WebApplicationContextUtils to get the Spring application context, with this Spring application context, you are able to access and get beans from Spring container.


package com.mkyong.rest;

import javax.servlet.ServletContext;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.mkyong.customer.CustomerBo;

@Path("/customer")
public class PrintService {

	CustomerBo customerBo;

	@GET
	@Path("/print")
	public Response printMessage(@Context ServletContext servletContext) {

		//get Spring application context
		ApplicationContext ctx = 
                     WebApplicationContextUtils.getWebApplicationContext(servletContext);
		customerBo= ctx.getBean("customerBo",CustomerBo.class);
		
		String result = customerBo.getMsg();

		return Response.status(200).entity(result).build();

	}

}

3.2 Implement ApplicationContextAware

The second solution is create a class implement Spring ApplicationContextAware, and make it singleton, to prevent instantiation from other classes.


package com.mkyong.context;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class SpringApplicationContext implements ApplicationContextAware {

	private static ApplicationContext appContext;

	// Private constructor prevents instantiation from other classes
        private SpringApplicationContext() {}
    
	@Override
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		appContext = applicationContext;

	}

	public static Object getBean(String beanName) {
		return appContext.getBean(beanName);
	}

}

Remember register this bean, otherwise, Spring container will not aware of this class.

File : applicationContext.xml


<beans xmlns="http://www.springframework.org/schema/beans"
	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 ">

	<bean class="com.mkyong.context.SpringApplicationContext"></bean>
	
	<bean id="customerBo" class="com.mkyong.customer.impl.CustomerBoImpl">
        </bean>

</beans>

In REST service, you can use the new singleton class – “SpringApplicationContext“, to get bean from Spring container.


package com.mkyong.rest;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;
import com.mkyong.context.SpringApplicationContext;
import com.mkyong.customer.CustomerBo;

@Path("/customer")
public class PrintService {

	CustomerBo customerBo;

	@GET
	@Path("/print")
	public Response printMessage() {

		customerBo = (CustomerBo) SpringApplicationContext.getBean("customerBo");
		
		String result = customerBo.getMsg();

		return Response.status(200).entity(result).build();

	}

}

4. Integrate RESTEasy with Spring

To integrate both in a web application, add Spring “ContextLoaderListener” in your web.xml.

File :web.xml


<web-app id="WebApp_ID" version="2.4"
	xmlns="http://java.sun.com/xml/ns/j2ee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
	http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	<display-name>Restful Web Application</display-name>
	
	<context-param>
		<param-name>resteasy.resources</param-name>
		<param-value>com.mkyong.rest.PrintService</param-value>
	</context-param>

	<listener>
		<listener-class>
			org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
                </listener-class>
	</listener>

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

	<servlet>
		<servlet-name>resteasy-servlet</servlet-name>
		<servlet-class>
			org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
                </servlet-class>
	</servlet>

	<servlet-mapping>
		<servlet-name>resteasy-servlet</servlet-name>
		<url-pattern>/*</url-pattern>
	</servlet-mapping>

</web-app>

5. Demo

Both solution 1 and 2 will generate following same output :

result
Note
Again, if you understand this RESTEasy Spring guide, or has a better solution, please share it to me.

Download Source Code

References

  1. Accessing Spring beans from legacy code
  2. General way to integrate Spring with others framework
  3. Better than nothing RESTEasy + Spring example
  4. ApplicationContextAware JavaDoc
  5. Wiki Singleton pattern in Java
  6. Get Spring Application context

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
16 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
ThinkingInJava
7 years ago

hanks for the nice tutorial. Can you please publish a tutorial on the following?

RESTEasy + Spring Security 4.x + OAuth2

It will really be helpful if you can publish it as I did not find any blog/tutorial on this combination on Web.

shasilva
3 years ago

//get Spring application context
ApplicationContext ctx =
WebApplicationContextUtils.getWebApplicationContext(servletContext);
customerBo= ctx.getBean(“customerBo”,CustomerBo.class);

To get the bean, is it possible to use @Asutowire or Inject bean to PrintService using getter setter with xml configurations

Suresh.A
10 years ago

Thanks a lot, it works for me. Its very simple steps to integrate spring into the rest-easy project

cmiranda
10 years ago

Thanks!
I’m having only one issue with this.. Injected beans are null.. all of them. I’m using CDI. How can I solve this?

Suresh.A
10 years ago
Reply to  cmiranda

If you have the bean null issue, do the following.

Add below dependency in pom.xml

org.jboss.resteasy
resteasy-spring
3.0.6.Final

Add the spring-resteasy connector in web.xml to solve the problem

org.jboss.resteasy.plugins.spring.SpringContextLoaderListener

Ignore, if you have already solved the problem

Rachit
8 years ago
Reply to  Suresh.A

I have resteasy-spring-2.2.1.GA.jar but it is not working. Do I have to add 3.0.6 version to make it work ?

Eduardo Silva
10 years ago

Thank you! These saved my day (I am using Jersey, but the very same principles – for injecting Spring – can be applied). I am using this (Spring+Jersey) to handle Container-managed EntityManager on Tomcat…

Few writing “mistakes” I found while reading the tutorial:
– the URL to “official RESTEasy guide to integrate Spring” is malformed
– “ApplicationContextAware intrefaces” is misspelled

Cheers!

AquaDrehz
11 years ago

I’ve some problem for run this example.
After I set the goal: clean jboss-as:deploy

The console output is below

[INFO] Scanning for projects…
[INFO] ————————————————————————
[INFO] BUILD FAILURE
[INFO] ————————————————————————
[INFO] Total time: 0.209s
[INFO] Finished at: Sun Feb 24 17:31:42 ICT 2013
[INFO] Final Memory: 4M/15M
[INFO] ————————————————————————
[ERROR] Error resolving version for plugin ‘URL=http://localhost’ from the repositories [local (C:\Users\AquaDrehz\.m2\repository), central (http://repo.maven.apache.org/maven2)]: Plugin not found in any plugin repository -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginVersionResolutionException

My topic on Jboss Community.
Reference: https://community.jboss.org/message/797896?tstart=0

Divash Ramani
11 years ago

Excellent topic appreciated the efforts. Thanks

?? ????
11 years ago

Rattling good info can be found on site . “Compassion for myself is the most powerful healer of them all.” by Theodore Isaac Rubin.

Jay
11 years ago

I am new to Restful service. I have gone through the java document – JAX-RS. Then I started looking your tutorial. Easy to understand and follow the steps.

I should appreciate your effort in coming up with this useful tutorial and helps others to understand easily.

Hemu
11 years ago

Your tutorials are Awesome :)Thanks a lot.

Garry
11 years ago

Amazig.. short, clear, direct and works 😉

Thanx

MP
11 years ago

Your site is amazing – thank you so much. All your examples are easy to follow and such an incredible resource here – thanks.

tumit
12 years ago

Thank you very much for clean and clear example 🙂

Dinesh jakhar
10 years ago

How to intergrate Restful web Services with Struts2 ?