Java Reference
In-Depth Information
Click here to view code image
List<Variant> vs =
Variant.mediatypes("application/xml", "application/json")
.languages("en", "fr").build();
This code snippet calls the build method of the VariantListBuilder class. The
VariantListBuilder class is invoked when you call the mediatypes , lan-
guages , or encodings methods. The build method builds a series of resource rep-
resentations. The Variant list created by the build method has all possible combina-
tions of items specified in the mediatypes , languages , and encodings methods.
In this example, the size of the vs object as defined in this code snippet is 4, and the con-
tents are as follows:
Click here to view code image
[["application/xml","en"], ["application/json","en"],
["application/xml","fr"],["application/json","fr"]]
The javax.ws.rs.core.Request.selectVariant method accepts a list of
Variant objects and chooses the Variant object that matches the HTTP request. This
method compares its list of Variant objects with the Accept , Accept-Encoding ,
Accept-Language , and Accept-Charset headers of the HTTP request.
The following code snippet shows how to use the selectVariant method to select the
most acceptable Variant from the values in the client request.
Click here to view code image
@GET
public Response get(@Context Request r) {
List<Variant> vs = ...;
Variant v = r.selectVariant(vs);
if (v == null) {
return Response.notAcceptable(vs).build();
} else {
Object rep = selectRepresentation(v);
return Response.ok(rep, v);
}
}
The selectVariant method returns the Variant object that matches the request, or
null if no matches are found. In this code snippet, if the method returns null, a Response
object for a non-acceptable response is built. Otherwise, a Response object with an OK
Search WWH ::




Custom Search