RESTEasy – Share Context Data with ResteasyProviderFactory

Many times we get into situation where we have to pass data from-to multiple layers in our application. An example can be application using interceptors. Suppose we have two interceptors in our application, one for login check and second for putting audit information in database. We want to use the User object from first interceptor, into second interceptor.

In RESTEasy based applications, above functionality can be easily implemented using ResteasyProviderFactory instance. ResteasyProviderFactory is enabled by adding a filter in web.xml file using below definition:


Now you can easily get the instance of ResteasyProviderFactory in any class in scope of RESTEasy context. So, to share data between interceptors, you will need to do two steps:

1)Set the data in context map

This context data is set using pushContext() method. This method add the data to a thread local stack defined as:

protected static ThreadLocalStack<Map<Class<?>, Object>> contextualData 
						= new ThreadLocalStack<Map<Class<?>, Object>>();

You need to push data as follows:

User user = new User();
//Set some user attributes

//Get registered ResteasyProviderFactory instance
ResteasyProviderFactory factory = ResteasyProviderFactory.getInstance();

//Add user into context data map
factory.pushContext(User.class, user);

2)Get the data from context map

Getting back the data set in first step is very simple. Use popContextData() method. This context data is thread-local in nature so you do not need to worry about getting wrong data when retrieving in another place in your code.

 ResteasyProviderFactory factory = ResteasyProviderFactory.getInstance();

This way you can use ResteasyProviderFactory to share data between multiple layers in your application.

Happy Learning !!

Was this post helpful?

Join 7000+ Awesome Developers

Get the latest updates from industry, awesome resources, blog updates and much more.

* We do not spam !!

3 thoughts on “RESTEasy – Share Context Data with ResteasyProviderFactory”

  1. java.lang.ClassCastException: org.jboss.resteasy.spi.ResteasyProviderFactory cannot be cast to org.jboss.resteasy.spi.ResteasyProviderFactory
    at org.jboss.resteasy.spi.ResteasyProviderFactory.getInstance(
    at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(
    at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(
    at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(
    at javax.servlet.http.HttpServlet.service(
    at org.apache.catalina.core.StandardWrapper.service(
    at org.apache.catalina.core.StandardWrapperValve.invoke(
    at org.apache.catalina.core.StandardContextValve.invoke(
    at org.apache.catalina.core.StandardPipeline.doInvoke(
    at org.apache.catalina.core.StandardPipeline.invoke(
    at org.apache.catalina.core.StandardHostValve.invoke(
    at org.apache.catalina.connector.CoyoteAdapter.doService(
    at org.apache.catalina.connector.CoyoteAdapter.service(
    at com.sun.grizzly.http.ProcessorTask.invokeAdapter(
    at com.sun.grizzly.http.ProcessorTask.doProcess(
    at com.sun.grizzly.http.ProcessorTask.process(
    at com.sun.grizzly.http.DefaultProtocolFilter.execute(
    at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(
    at com.sun.grizzly.DefaultProtocolChain.execute(
    at com.sun.grizzly.DefaultProtocolChain.execute(
    at com.sun.grizzly.http.HttpProtocolChain.execute(
    at com.sun.grizzly.ProtocolChainContextTask.doCall(
    at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(
    at com.sun.grizzly.util.AbstractThreadPool$

    • It’s class loader issue. Basically RESTEasy jars are available in two places in classpath. If you are using JBoss, then RESTEasy jars should be present in jboss distribution, so don’t include them in your war file.


Leave a Comment


A blog about Java and related technologies, the best practices, algorithms, and interview questions.