uCosminexus Application Server, Web Service Development Guide

[Contents][Glossary][Index][Back][Next]

24.4.2 javax.ws.rs.core.HttpHeaders

The javax.ws.rs.core.HttpHeaders contains the HTTP header of an HTTP request.

The following example shows the usage of javax.ws.rs.core.HttpHeaders that is injected into the field of a root resource class:

package com.sample.resources;
 
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
 
//Root resource class
@Path("/root")
public class Resource {
  //A field in which the HttpHeaders is injected by using the Context annotation
  private @Context HttpHeaders httpHeaders;
 
//Resource method
  @GET
  public String getValue () {
    String value = this.httpHeaders.getRequestHeader("Accept").get(0);
    return value;
  }
}

Consider the context root of the Web application (WAR file) containing the root resource class com.sample.resources.Resource to be example, and that the Web application is published on a host named sample.com. In the above example, with the HTTP GET request corresponding to the URL http://sample.com/example/root wherein application/xml is specified in the HTTP Accept header, first, the javax.ws.rs.core.HttpHeaders context is injected into the httpHeaders field and then the getValue() method that can process the HTTP GET request is called. Therefore, if you acquire the Accept-HTTP header value from the httpHeaders field by using the getValue() method, the application/xml value is acquired.