What Is javax.ws.rs.core.context? (Part 4)? [Snippets]
Continuing with our journey of the @Context annotation, let's see how you can inject Servlet request and response classes into your Java EE projects.
Join the DZone community and get the full member experience.
Join For FreeIn part 3 of What Is javax.ws.rs.core.context?, you learned how to use the @Context annotation with Request and Configuration, Providers, and Application instances.
In this article, you will learn about using the @Context annotation to inject the HttpServletResponse and the HttpServletRequest classes.
Get Access to the HttpServletRequest Properties
The JAX-RS API runs on top of Servlets and therefore instances of servlet objects are available to the JAX-RS resource. 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.
@Path("/remote-address")
public class HttpServletRequestResource {
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getRemoteAddress(
final @Context HttpServletRequest httpServletRequest){
return Response.ok(httpServletRequest.getRemoteAddr()).build();
}
}
In this code example, the Servlet request object is injected into the method parameter httpServletRequest by the @Context annotation. The getRemoteAddr() method is called and returns the IP address of the server that made the request.
If you are running this example on a local machine the response from calling the URL http://localhost:8080/rest-server/remote-address will be 127.0.0.1.
Get Access to the HttpServletResponse Properties
Just as you can obtain an instance of the HttpServletRequest object you can also get the HttpServletResponse instance and call its methods and set values on the response instance.
Let’s have a look at an example that gets the ServletOutputStream and flush a message to the response.
@Path("/output")
public class HttpServletResponseResource {
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response get(
final @Context HttpServletResponse httpServletResponse)
throws IOException {
ServletOutputStream out = httpServletResponse.getOutputStream();
out.print("Hello");
out.flush();
return Response.ok().build();
}
}
In this example, the HttpServletResponse object instance is injected into the method parameter httpServletResponse and then an instance of the ServletOutputStream object is obtained. I then use this object to write a message to the output stream and then flush it to the response.
If you visit the URL http://localhost:8080/rest-server/output you will see the message “Hello” printed to the screen.
Code Repository
The source code for this and all my articles are in the readlearncode_articles Github repository.
What Next?
That is all for part 4, in part 5 of What is javax.ws.rs.core.context? you will learn how to use the @Context annotation to inject instances of javax.servlet.ServletConfig and javax.servlet.ServletContext.
Further Reading
I have recently posted a mini-series of blogs taking a look at JAX-RS. They are published on readlearncode.com and discuss how to manage bean validation failure, work with Consumers and Producers, and how to create JAX-RS Resource Entities.
Published at DZone with permission of Alex Theedom, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments