Is it possible in Jersey to have access to an injected HttpServletRequest, instead of to a proxy
Don't make your resource class a singleton. If you do this, there is no choice but to proxy, as the request is in a different scope.
@Singleton
@Path("servlet")
public class ServletResource {
@Context
HttpServletRequest request;
@GET
public String getType() {
return request.getClass().getName();
}
}
With @Singleton
C:\>curl http://localhost:8080/api/servlet
com.sun.proxy.$Proxy41
Without @Singleton
C:\>curl http://localhost:8080/api/servlet
org.eclipse.jetty.server.Request
There are other ways your class can become a singleton, like registering it as an instance
You can aslo inject it as a method parameter. Singleton or not, you will get the actual instance
@GET
public String getType(@Context HttpServletRequest request) {
return request.getClass().getName();
}
See Also
- Injecting Request Scoped Objects into Singleton Scoped Object with HK2 and Jersey