Java Reference
In-Depth Information
Accessing HTTP Headers
Problem
You want your JAX-RS service to access the HTTP headers sent with a request.
Solution
Access the headers using the HttpHeaders class as a parameter to the method in which you
want to work with them, annotating the parameter with @Context .
Alternatively, use the @HeaderParam annotation.
Discussion
Example 8-27 illustrates how to access HTTP headers using the @Context annotation.
Example8-27.Accessing HTTP headers using the @Context annotation
package com.soacookbook.rest.headers;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MultivaluedMap;
@Path("headers")
public class Headers {
@GET
public String doGet(@Context HttpHeaders headers) {
System.out.println("Looking at headers.");
//list all incoming headers
MultivaluedMap<String,String> map = headers.getRequestHeaders();
for (String header : map.keySet()) {
System.out.println(header + "=" + map.get(header));
}
return "All done";
}
}
Search WWH ::




Custom Search