Jersey REST service and Thread Local usage

In this exercise, I am going to provide an example to show how we can use the ThreadLocal with the Jersey REST service.

Consider that it’s a REST application and we need the user details in the application flow many places. The User details will be retrieved with the header information availbale in the HttpServletRequest.
One solution is to pass the HttpServletRequest object whereever its required. But its a tedious job.
So an another way is to use the ContainerRequestFilter and processes the request, then extract out the header and call the back end to get the User details and put into the ThreadLocal storage so that it can be accessible anywhere in that thread flow.

Refer the below sample code to know how to do that. We don’t use the Spring Framework here.

This class holds the static ThreadLocal variable and it has getter and setter for User object. We will be setting the User Object from the ContainerRequestFilter

package com.utils;

public class DataHolder{

    private static final ThreadLocal userThreadLocal = new ThreadLocal();

    public static void setUser(User user) {

    public static User getUser() {
        return userThreadLocal.get();

    public static void remove() {

This filter will intercept the request and extract out the header and retrieve the user details and put it into the ThreadLocal.

package com.filters;

import javax.servlet.http.HttpServletRequest;

public class UserDataCaptureRequestFilter implements ContainerRequestFilter {
    private HttpServletRequest httpRequest;

    public void filter(ContainerRequestContext requestContext) throws IOException {
        //Get the User data from the back end system with the request header/cookie. I have not given the getUser() method but assume it will get the user details 
        User user = getUser(httpRequest);

The ResourceConfig class where we can specify the packages to look into for the end points and context

public class Application extends ResourceConfig {
    public Application() {
        packages(true, "com.resource");
        packages(true, "com.filters");

This service returns the User details in JSON format. Here We have not retrived the User Details once again since we already retrieved the data in the filter itself. So the data would be available in the Thread scope. So we could use it

package com.resource;

import com.utils.DataHolder;

public class UserResource{

    public Response getUser() {
        return Response.ok().entity(DataHolder.getUser()).type(MediaType.APPLICATION_JSON).build();



2 thoughts on “Jersey REST service and Thread Local usage

  1. Hey Bala,
    If you use “private static final ThreadLocal userThreadLocal = new ThreadLocal();” then you can will not need to create “setUser” and “getUser” wrapper methods. You can directly use “set” and “get” and ThreadLocal .

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s