Java Reference
In-Depth Information
GET http://example.com/stuff
Accept-Encoding: gzip;q=1.0, compress;0.5; deflate;q=0.1
Here,
gzip
is desired first, then
compress
, followed by
deflate
. In practice, clients use the
Accept-Encoding
header to tell the server which encoding formats they support, and they
really don't care which one the server uses.
When a client or server encodes a message body, it must set the
Content-Encoding
header.
This tells the receiver which encoding was used.
JAX-RS and Conneg
The JAX-RS specification has a few facilities that help you manage conneg. It does method
dispatching based on
Accept
header values. It allows you to view this content information
directly. It also has complex negotiation APIs that allow you to deal with multiple decision
points. Let's look into each of these.
Method Dispatching
In previous chapters, we saw how the
@Produces
annotation denotes which media type a
JAX-RS method should respond with. JAX-RS also uses this information to dispatch re-
quests to the appropriate Java method. It matches the preferred media types listed in the
Ac-
cept
header of the incoming request to the metadata specified in
@Produces
annotations.
Let's look at a simple example:
@Path
(
"/customers"
)
public
public class
class
CustomerResource
CustomerResource
{
@GET
@Path
(
"{id}"
)
@Produces
(
"application/xml"
)
public
public
Customer
getCustomerXml
(
@PathParam
(
"id"
)
int
int
id
) {...}
@GET
@Path
(
"{id}"
)
@Produces
(
"text/plain"
)
public
public
String
getCustomerText
(
@PathParam
(
"id"
)
int
int
id
) {...}
@GET
@Path
(
"{id}"
)
@Produces
(
"application/json"
)
public
public
Customer
getCustomerJson
(
@PathParam
(
"id"
)
int
int
id
) {...}
}