I’m building an API using Jersey on the Glassfish 3.1 server and need to get access to the HttpServletRequest object in order to get certain headers, caller’s ip, etc. I could just inject it into every API method call but it seems more efficient to just do it globally. Is it safe to inject it at the class level like in the snippet below or will this cause some kind of concurrency issues with Glassfish?
@Path("/myapi")
@RequestScoped
public class MyApiResource {
@Context private UriInfo context;
@Context private HttpServletRequest request;
It is safe. Don’t use the @RequestScoped annotation – JAX-RS resources are request scoped by default. That means a new instance gets created for each request hence no concurrency issues.