Java Reference
In-Depth Information
The @Context annotation gives you access to a variety of other contextual variables too, in-
cluding UriInfo , SecurityContext , ContextResolver , and ServletContext . The Multi-
valuedMap interface is part of JAX-RS, and allows you to specify more than one value for
the same key. This is unlike a standard Java Map, which would just overwrite any new values
added for the same key.
In my case, this code prints the following:
Looking at headers.
accept-encoding=[gzip, deflate]
cache-control=[no-cache]
connection=[Keep-Alive]
host=[localhost:8080]
accept-language=[en-us]
user-agent=[Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR
2.0.50727)]
ua-cpu=[x86]
accept=[*/*]
Using @HeaderParam
Another way to set and get the value of a specific field is to use the @HeaderParam annotation.
The class in Example 8-28 illustrates setting a custom response header and also shows how to
examine the value of the User-Agent header.
Example8-28.Using the @HeaderParam annotation
package com.soacookbook.rest.header;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
@Path("/header")
@Produces("text/html")
public class HeaderService {
//set custom header
@GET
@Path("/home")
public Response home() {
System.out.println("Header Home page.");
String html = "<html><body><a href='step2'>Click</a> " +
Search WWH ::




Custom Search