RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


JAX-RS: Developing RESTful Web Services in Java : Page 5

Learn how the JAX-RS API and annotations enable you to develop REST-based web services in Java.



To help you get a handle on the deployment environment and the individual requests, JAX-RS provides different types of context to resource classes and providers. You use the annotation @Context to get the Java types related to a request or response. These are the different types of context JAX-RS provides:
  • UriInfo: UriInfo provides information about the components of a request URI, whether static or dynamic. However, the information provided is valid only per request. An instance of UriInfo is injected into the field of a class or into the parameter of a method using @Context.
    public class SayHello {
        public SayHello() {
        public String getHtml(@Context UriInfo uri) {
            MultivaluedMap<String, String> map = uri.getQueryParameters();
            Set<String> set = map.keySet();
            Iterator<String> iterator = set.iterator();
            String content = "";
            while (iterator.hasNext()) {
                content = content + map.get(iterator.next()) + "*******";
            return content;
    The above example uses context to inject an object of type UriInfo as the method parameter whose methods provide access to request URI information.
  • Headers: HttpHeaders provide information about a request header. Generally, the information is provided in the form of a map. An instance of HttpHeader is injected into the field of a class or into the parameter of a method using @Context. The methods of HttpHeaders provide access to request information.
    public String getHeaders(@Context HttpHeaders headers) {
  • Request: The request interface is provided for negotiating content and evaluating certain preconditions. A request instance is injected into the field of a class or into the parameter of a method using @Context.
  • Security: SecurityContext provides information about the security context of the request. With SecurityContext, you can retrieve information about the user, group, and roles from the request. It also helps in identifying the authentication scheme used and determining whether the request is using HTTPS or not. An instance of SecurityContext is injected into the field of a class or into the parameter of a method using the @Context annotation.

All the above contexts are available to all containers where a JAX-RS root resource class or a provider is deployed. Apart from these standard ones, you can use @Context to specify container-specific resources too. For example, in a Servlet container-based environment, you can use @Context to inject dependencies of type HttpServletRequest, HttpServletResponse, ServletConfig, and ServletContext.

Table 5 lists the Java annotations defined by JAX-RS.

Table 5. List of Annotations Defined as Part of JAX-RS

Simple REST-Based Web Services

By defining annotations for creating and consuming REST-based web services, JAX-RS provides good support for building web services in a simple and elegant way. Using the JAX-RS API and annotations, you can expose a POJO as a web service and any request coming to a web resource will be served by a resource class and resource methods. JAX-RS also has the added benefit of sending and receiving objects of any type.

Acknowledgements: The author would like to sincerely thank Mr. Subrahmanya, SV, VP, ECOM Research Group, E&R, for ideas, guidance, support, and constant encouragement and Mr. Parameswaran Seshan, Senior Technical Architect, ECOM Research Group, E&R, for kindly reviewing this article.

Sangeetha S. works as a technical architect at E-Commerce Research Labs at Infosys Technologies. She has more than 14 years of experience in the design and development of Java and Java EE applications. She has co-authored a book on J2EE Architecture and also has written articles for JavaWorld and java.net.
Email AuthorEmail Author
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date