Java Reference
In-Depth Information
@GET
@Produces
(
"text/html"
)
public
public
String
get
(
@HeaderParam
(
"Referer"
)
String referer
) {
...
}
}
The
@HeaderParam
annotation is pulling the
Referer
header directly from the HTTP request
and injecting it into the referer method parameter.
Raw Headers
Sometimes you need programmatic access to view all headers within the incoming request.
For instance, you may want to log them. The JAX-RS specification provides the
javax.ws.rs.core.HttpHeaders
interface for such scenarios.
public
public interface
interface
HttpHeaders
HttpHeaders
{
public
public
List
<
String
>
getRequestHeader
(
String name
);
public
public
MultivaluedMap
<
String
,
String
>
getRequestHeaders
();
...
}
The
getRequestHeader()
method allows you to get access to one particular header, and
getRequestHeaders()
gives you a map that represents all headers.
As with
UriInfo
, you can use the
@Context
annotation to obtain an instance of
HttpHead-
ers
. Here's an example:
@Path
(
"/myservice"
)
public
public class
class
MyService
MyService
{
@GET
@Produces
(
"text/html"
)
public
public
String
get
(
@Context
HttpHeaders headers
) {
String referer
=
headers
.
getRequestHeader
(
"Referer"
).
get
(
0
);
for
for
(
String header
:
headers
.
getRequestHeaders
().
keySet
())
{
System
.
out
.
println
(
"This header was set: "
+
header
);
}
...
}
}