0

I'm new to JAX-RS and I'm trying to understand how the @Context annotation works. I have a REST service and am currently using certain filters to do extra processing for different request types. Inside one of those filters is the following line:

public class SentryFilter {
     @Context
     HttpServletResponse response;

...

This value is used later on in the filter() method of that filter. The problem is that the response object is null. I've stepped through a debugger and can't determine why it's null.

From what I've read in the JAX-RS documentation, the @Context annotation for HttpServletResponse can be filled by the resource methods. So, I modified my the API I'm calling to include HttpServletResponse

public interface APIStuff {
    @Path("deviceName")
        @GET
        @Sentry
        String getDeviceName(@PathParam("deviceId") @Size(min = 1, max = 1024) final String deviceId, @Context HttpServletResponse httpServletResponse);

    ...

This returns the same HttpServletResponse is null error.

So the question is, where is this value supposed to be "filled"? The person who wrote the filter class obviously did so with the belief that the response object would be filled, so I don't think it's a matter of passing in @Context HttpServletResponse to the filter() method.

Jeremy Fisher
  • 2,510
  • 7
  • 30
  • 59

3 Answers3

1

The @Context annotation allows you to inject request/response context details into JAX-RS provider and resource classes. Injection can be performed into a class field, a bean property or a method parameter.

The types available for injection are listed in this answer.


You haven't provided any details about how your application is deployed, but be aware that HttpServletResponse is available for injection only when the application is deployed in servlet containers (Tomcat, for example). It might be your problem.

cassiomolin
  • 124,154
  • 35
  • 280
  • 359
1

@Context varies with usage and depends on what is actually being injected. If you are injecting the HttpServletResponse, it needs to be part of the method signature. This is because most beans are implemented as a singleton, and having multiple requests will just override the response if implemented at an instance level.

As an aside, IMO the API should support injecting the method-only '@Context' resources into a (for example) ThreadLocal<HttpServletResponse>

Your Filter that you posted does not extend javax.servlet.Filter, are you including the full signature? how is this filter being used?

As to why your resource method level injections are turning up null, are you sure it's actually being called within the servlet context?

Jeff Wang
  • 1,837
  • 1
  • 15
  • 29
1

As @cassiomolin said, you haven't provided any details about how your application is deployed, HttpServletResponse is available for injection only when the application is deployed in servlet containers (Tomcat, for example). It might be your problem.

I had similar problem with GrizzlyHttpServer. Cause of my issue was, Grizzly provides similar abstractions to those offered by the Servlet specification: HttpHandler (Servlet), Request (HttpServletRequest), Response (HttpServletResponse).

So I had to use: import org.glassfish.grizzly.http.server.Request;
instead of: import javax.servlet.http.HttpServletRequest;

user3234777
  • 303
  • 2
  • 8