Java Reference
In-Depth Information
"to go to the next step.</body></html>";
//include header in 200 OK response
Response response = Response.ok(html).header(
"X-Powered-By", "JAX-RS/Jersey").build();
System.out.println("Built response.");
return response;
}
//retrieve standard header
@GET
@Path("/step2")
public String step2(@HeaderParam("User-Agent") String agent) {
System.out.println("Header Step 2.");
String html = "";
if (agent != null) {
System.out.println("Header Value: " + agent);
html = "<html><body>Your User-Agent is: " +
agent + "</body></html>";
} else {
html = "<html><body>Could not find header.
:(</body></html>";
}
System.out.println("Sending response for step2.");
return html;
}
}
This class does two things with headers. First, it sets a custom header that you create. The
name of the header is “X-Powered-By” and the value is “JAX-RS/Jersey”. This header will
get returned to the client for the current request, but it won't persist across subsequent invoc-
ations automatically. The key here is that you use the Response builder pattern to create a re-
sponse template representing 200 OK, and then add the header key/value pair to your response
object using the header method. Once your response is prepared, call the build method to fi-
nalize it.
Search WWH ::




Custom Search