Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is @javax.ws.rs.core.Context

First of all, I am very new to this topic and not sure if its a very basic question. I couldnt help but posting here.

I am looking at a code that uses restful webservice. An ajax call is being made inorder to provide details to this ws. The method signature looks like this:

@Path("/issues")
@GET
public Response getIssueCockpit(@javax.ws.rs.core.Context HttpServletRequest paramHttpServletRequest, @QueryParam("filterGlobal") String paramString) throws Exception 
{ 
    //Code here
}

I understand that the webservice caller calls this API using "eg: http://app/resource/issues1" and this method gets called.

  1. Can you please help me understand what is @javax.ws.rs.core.Context HttpServletRequest paramHttpServletRequest in the below method call.
  2. What does the annotation do in this case.
  3. An ajax call is being made to provide details to this WS> How is the context preserved in the ServletRequest.

Thanks for the help

like image 218
Awanish Avatar asked Oct 28 '15 06:10

Awanish


People also ask

What is javax WS RS core application?

Defines the components of a JAX-RS application and supplies additional metadata. A JAX-RS application or implementation supplies a concrete subclass of this abstract class. The implementation-created instance of an Application subclass may be injected into resource classes and providers using Context . java.

What is javax WS RS package?

A collection of built-in priority constants for the JAX-RS components that are supposed to be ordered based on their javax. annotation. Priority class-level annotation value when used or applied by JAX-RS runtime.

What is the use of @context annotation?

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.

What is @context HttpServletRequest?

The @Context annotation is used to inject the HttpServletRequest instance for the current request. Its methods give access to detailed information about the request. Let's look at a simple example that retrieves the request's remote address.


2 Answers

If you've worked with an dependency injection frameworks like Spring or CDI, you'll see that in order to let the framework inject a dependency, you need a marker annotation. In Spring you would see @Autowired or @Inject, in CDI you would see @Inject. @Context works the same way. In order for the JAX-RS runtime to know that HttpServletRequest is to be injected, it needs to be annotated with @Context. Same way JAX-RS knows to inject the query parameter is through the @QueryParam annotation.

The HttpServletRequest is from the servlet container. When a request comes the container creates the HttpServletRequest and passes it down to servlet implementations. The JAX-RS runtime hands this object to your resource method/class, if it sees that you want it, by annotating it.

like image 125
Paul Samsotha Avatar answered Oct 18 '22 00:10

Paul Samsotha


The HttpServletRequest object represents the HTTP request from the browser or client application. So a call to "http://app/resource/issues1" is represented by an instance of the HttpServletRequest. This object has methods that report information about the request such as the Http headers, Media type, and the request body.

The annotation @Context injects (just like @Autowired from Spring and @Inject from Java EE) the HttpServletRequest instance for the request made to the path (/issue) with HTTP method type (GET). In fact, the @Context annotation can inject a vast number of very useful objects related to the request. See the full list below:

  • HttpHeaders -> HTTP header parameters and values
  • UriInfo -> Captures path variables and query parameters
  • SecurityContext -> Provides access to security-related information for a request
  • ResourceContext -> Provides access to instances of resource classes
  • Request -> Precondition request processing
  • Application, Configuration, and Providers -> Provide information about the JAX-RS application environment
  • HttpServletRequest -> Provides access to the HttpServletRequest instance
  • HttpServletResponse -> Provides access to the HttpServletResponse instance
  • ServletConfig -> Provides access to the ServletConfig
  • ServletContext -> Provides access to the ServletContext

The ServletRequest lives for as long as the request exists. This is normally very short-lived so for the duration of the request the ServletRequest is maintaining.

like image 31
Alex Theedom Avatar answered Oct 18 '22 00:10

Alex Theedom