You are on page 1of 1

7.

Configuration Scopes:
If we 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. We can also override
or add registered components 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 we look at the properties of target1 we can find the same properties as those
defined on the client instances, as WebTargets inherit their configuration from the
Client or WebTarget they were created from. The target2 variable overrides the
username property and registers a provider specifically for target2. So, target2’s
configuration properties and registered components will be a little bit different than
target1’s. This way of configuration scoping makes it much easier for you to share
initialization code so we can avoid creating a lot of extra objects we don’t need and
reduce the amount of code you have to write.
Wrapping Up:
In this chapter, we learned about the JAX-RS Client API. Client interfaces manage
global configuration and the underlying socket connections we’ll use to communicate
with a service. WebTarget represents a URI that you can build and invoke HTTP
requests from.

You might also like