Java Reference
In-Depth Information
builder
.
type
(
v
.
getMediaType
())
.
language
(
v
.
getLanguage
())
.
header
(
"Content-Encoding"
,
v
.
getEncoding
());
return
return
builder
.
build
();
}
You interact with
VariantListBuilder
instances by calling the
mediaTypes()
,
lan-
guages()
, and
encodings()
methods. When you are done adding items, you invoke the
build()
method and it generates a
Variant
list containing all the possible combinations of
items you built it with.
You might have the case where you want to build two or more different combinations of
variants. The
VariantListBuilder.add()
method allows you to delimit and differentiate
between the combinatorial sets you are trying to build. When invoked, it generates a
Vari-
ant
list internally based on the current set of items added to it. It also clears its builder state
so that new things added to the builder do not combine with the original set of data. Let's
look at another example:
Variant
.
VariantListBuilder
vb
=
Variant
.
VariantListBuilder
.
newInstance
();
vb
.
mediaTypes
(
MediaType
.
APPLICATION_XML_TYPE
,
MediaType
.
APPLICATION_JSON_TYPE
)
.
languages
(
new
new
Locale
(
"en"
),
new
new
Locale
(
"es"
))
.
encodings
(
"deflate"
,
"gzip"
)
.
add
()
.
mediaTypes
(
MediaType
.
TEXT_PLAIN_TYPE
)
.
languages
(
new
new
Locale
(
"en"
),
new
new
Locale
(
"es"
),
new
new
Locale
(
"fr"
))
.
encodings
(
"compress"
);
In this example, we want to add another set of variants that our JAX-RS method supports.
Our JAX-RS resource method will now also support
text/plain
with English, Spanish, or
French, but only the
compress
encoding. The
add()
method delineates between our original
set and our new one.
You're not going to find a lot of use for the
Request.selectVariant()
API in the real
world. First of all, content encodings are not something you're going to be able to easily
work with in JAX-RS. If you wanted to deal with content encodings portably, you'd have to
do all the streaming yourself. Most JAX-RS implementations have automatic support for en-
codings like GZIP anyway, and you don't have to write any code for this.
Second, most JAX-RS services pick the response media type automatically based on the
@Produces
annotation and
Accept
header. I have never seen a case in which a given lan-
guage is not supported for a particular media type. In most cases, you're solely interested in