What is javax.ws.rs.core.context? [ Part 5 ]
How to use the @Context annotation
In part 4 of What is javax.ws.rs.core.context? you learned about using the @Context annotation to inject the HttpServletResponse and the HttpServletRequest classes into resource methods.
In this article, you will learn about using the remaining two classes that are only available in a servlet container, they are: javax.servlet.ServletConfig and javax.servlet.ServletContext.
Query the Servlet Container’s Configurations in ServletConfig
The Servlet container contains configurations that may be of interests to a REST endpoint developer and so you can get access to the javax.servlet.ServletConfig instance by injecting it using the @Context annotation.
Let’s jump into a simple example:
@Path("servlet-config") public class ServletConfigResource { @GET @Produces(MediaType.APPLICATION_JSON) public Response getServletName( final @Context ServletConfig servletConfig){ return Response.ok(servletConfig.getServletName()).build(); } }
This code snippet retrieves the Servlet’s name from the injected ServetConfig instance and returns it to the calling client. In my case, the name of the servlet returned is com.readlearncode.RESTConfig, when I visit the URL http://localhost:8080/rest-server/servlet-config.
What’s in the ServletContext?
The final servlet related instance I want to look at is the ServletContext object. The servlet context provides access to many properties and method such as servlet configuration and dynamic servlet and filter configuration. Let’s look at an example.
@Path("servlet-context") public class ServletContextResource { @GET @Produces(MediaType.APPLICATION_JSON) public Response getContextPath( final @Context ServletContext servletContext) { return Response.ok(servletContext.getContextPath()).build(); } }
In the code above the ServletContext is injected into the method parameter servletContext. Then the context path of the current servlet context is retrieved and sent back to the client as a response. If you visit the URL http://localhost:8080/rest-server/servlet-context you will see the path /rest-server displayed.
Code Repository
The source code for this and all my articles are in the readlearncode_articles Github repository.
Conclusion
This concludes the tour of all the ways the @Context (javax.ws.rs.core.context) annotation can be used within your JAX-RS application. If you want to review all the parts to this series they are linked below:
- What is javax.ws.rs.core.context? [ Part 1 ]
- What is javax.ws.rs.core.context? [ Part 2 ]
- What is javax.ws.rs.core.context? [ Part 3 ]
- What is javax.ws.rs.core.context? [ Part 4 ]
- What is javax.ws.rs.core.context? [ Part 5 ]
What Next?
I have recently published an introductory course on Java EE covering all the most important APIs for those just starting out on a career in Enterprise Java. It is video course entitled Learning Java Enterprise Edition and during the 2-hour course, I introduce the most commonly used Java Enterprise Edition APIs. I demonstrate how to use those APIs with plenty of code examples and challenges to help the learner developer his/her skills.
For the learner with ambitions, there are courses teaching how to build a RESTful endpoint using JAX-RS, a course on how to construct a chat app with WebSockets and a course covering the JSON-Processing API course. The road map is full of Java EE courses covering the entire gambit of this extensive platform.
Further Reading
I regularly blog about Java EE on my blog readlearncode.com where I have recently published a mini-series of articles on the JAX-RS API.
Among the articles, there are discussions on bean validation failure in REST endpoints, how to work with Consumers and Producers, and how to create JAX-RS Resource Entities.
Reference: | What is javax.ws.rs.core.context? [ Part 5 ] from our JCG partner Alex Theedom at the Read Learn Code blog. |