Java Reference
In-Depth Information
try
try
{
customer
=
target
.
accept
(
"application/json"
)
.
get
(
Customer
.
class
);
}
catch
catch
(
RedirectionException redirect
) {
iif
(
redirected
)
throw
throw
redirect
;
redirected
=
true
true
;
target
=
client
.
target
(
redirect
.
getLocation
());
}
}
while
while
(
customer
==
null
null
);
In this example, we loop if we receive a redirect from the server. The code makes sure that
we allow only one redirect by checking a flag in the catch block. We change the
WebTarget
in the catch block to the
Location
header provided in the server's response. You might want
to massage this code a little bit to handle other error conditions, but hopefully you get the
concepts I'm trying to get across.
Configuration Scopes
If you look at the declarations of
ClientBuilder
,
Client
,
WebTarget
,
Invocation
, and
Invocation.Builder
, you'll notice that they all implement the
Configurable
interface.
Each one of these interfaces has its own scope of properties and registered components that it
inherits from wherever it was created from. You can also override or add registered compon-
ents or properties for each one of these components. For example:
Client client
=
ClientBuilder
.
newBuilder
()
.
property
(
"authentication.mode"
,
"Basic"
)
.
property
(
"username"
,
"bburke"
)
.
property
(
"password"
,
"geheim"
)
.
build
();
WebTarget target1
=
client
.
target
(
"http://facebook.com"
);
WebTarget target2
=
client
.
target
(
"http://google.com"
)
.
property
(
"username"
,
"wburke"
)
.
register
(
JacksonJsonProvider
.
class
);
If you viewed the properties of
target1
you'd find the same properties as those defined on
the
client
instances, as
WebTargets
inherit their configuration from the
Client
or
WebTar-
get
they were created from. The
target2
variable overrides the
username
property and re-
gisters a provider specifically for
target2
. So,
target2
's configuration properties and re-
gistered components will be a little bit different than
target1
's. This way of configuration