Search code examples
springjerseyaopjersey-2.0spring-aop

Intercepting Jersey Resource Methods with Spring aop


I am trying to use spring aop to intercept my jersey resource methods. I want to implement api versioning, where I transform the request to the latest version, do some work in the resource method, and then transform the response to the requested api version.

I am using spring and jersey currently. I tried to use spring aop to intercept the jersey resource methods, and at first glance it appeared to be working. However, I noticed 1 problem.

Anything injected into the RootResource class via jersey (Ex. @Context HttpHeaders) is null when using spring aop. It is null both in the advice method as well as the resource method. When I disable aop, the properties are injected properly and accessible in the resource method.

Any ideas on what is happening and how to fix it?

RootResource class:

@Component
@Path("test")
public class RestService extends BaseService {

    @Autowired
    RestService(MyClass myclass) {
        super(myclass);
    }


    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("{param1}")
    public Integer get(@PathParam("param1") Integer param1) {
        // I can access myClass fine, but headers and context are null!
        return param1;
    }

}

BaseService class:

public abstract class BaseService {
    @Context
    public HttpHeaders headers;
    @Context
    protected ServletContext context;

    protected MyClass myClass;

    BaseService(MyClass myClass) {
        this.myClass = myClass;
    }
}

spring aop interceptor:

@Aspect
public class Test {

    @Around("execution(* biocode.fims.rest.services..*.*(..))")
    public Object transformRequest(ProceedingJoinPoint jp) throws Throwable {
        BaseService baseService = (BaseService) jp.getTarget();
        I can access myClass fine, but headers and context are null;
        Object val = jp.proceed();
        return val;
    }
}

Solution

  • okay, so it looks like this is a bug in jersey-spring3. Issue

    There is a pr to fix this, but with no comments. I'm trying to get this merged. Github PR