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.
@javax.ws.rs.core.Context HttpServletRequest paramHttpServletRequest in the below method call. Thanks for the help
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.
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.
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 @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.
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.
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With