Obtaining HTTP headers using HttpHeaders objects

Using Java™ API for RESTful Web Services (JAX-RS), you can use the HttpHeaders object to access request headers.

About this task

By using an injected HttpHeaders object with the JAX-RS runtime environment, the HTTP request headers information is known and available for modification. The @javax.ws.rs.core.Context annotation indicates that a context object is injected. The javax.ws.rs.core.HttpHeaders interface is the interface of the object that you want to inject.

Procedure

Results

The resource method uses information from the HTTP headers that is sent in the request to determine an appropriate response.

Example

The following example illustrates a resource that returns a different greeting depending on the setting of the Accept-Language header.

In the following code snippet, the @Context HttpHeaders parameter is added to the method signature. Notice that the @Context annotation and the type declaration are in the parameters list of the getGreeting method.
import java.util.List; 
import java.util.Locale; 
  
import javax.ws.rs.GET; 
import javax.ws.rs.Path; 
import javax.ws.rs.Produces; 
import javax.ws.rs.core.Context; 
import javax.ws.rs.core.HttpHeaders; 
import javax.ws.rs.core.MediaType; 
  
@Path("/contexttest") 
public class HelloWorldInMyLanguage { 

    /**
     *   @return Returns the string in the preferred language.
     */
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String getGreeting(@Context HttpHeaders httpHeaders) {
        List<Locale> locales = httpHeaders.getAcceptableLanguages();

        if (locales.size() == 0) {
            return "Hello!";
        }

        Locale locale = locales.get(0);
        if (locale.equals(Locale.FRENCH)) {
            return "Bonjour!";
        } else if (locale.equals(Locale.GERMAN)) {
            return "Guten Tag!";
        } else {
            return "Hello!";
        }
    }
} 
Alternatively, you can choose to declare the @Context HttpHeaders httpHeaders property as a field in the class; for example:
import java.util.List; 
import java.util.Locale; 
  
import javax.ws.rs.GET; 
import javax.ws.rs.Path; 
import javax.ws.rs.Produces; 
import javax.ws.rs.core.Context; 
import javax.ws.rs.core.HttpHeaders; 
import javax.ws.rs.core.MediaType; 
  
@Path("/contexttest") 
public class HelloWorldInMyLanguage { 

    @Context
    HttpHeaders httpHeaders;

    /**
     * @return Returns the string in the preferred language.
     */
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String getGreeting() {
        List<Locale> locales = httpHeaders.getAcceptableLanguages();

        if (locales.size() == 0) {
            return "Hello!";
        }

        Locale locale = locales.get(0);
        if (locale.equals(Locale.FRENCH)) {
            return "Bonjour!";
        } else if (locale.equals(Locale.GERMAN)) {
            return "Guten Tag!";
        } else {
            return "Hello!";
        }
    }
}



In this information ...


IBM Redbooks, demos, education, and more

(Index)

Use IBM Suggests to retrieve related content from ibm.com and beyond, identified for your convenience.

This feature requires Internet access.

Task topic    

Terms of Use | Feedback

Last updated: Feb 19, 2011 8:24:38 AM CST
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=v610web&product=was-nd-mp&topic=twbs_jaxrs_contextobjects_httpheaders
File name: twbs_jaxrs_contextobjects_httpheaders.html