Java Reference
In-Depth Information
The
ClientBuilder
class also has methods to configure SSL. We'll cover this in detail in
Client client
=
ClientBuilder
.
newBuilder
()
.
property
(
"connection.timeout"
,
100
)
.
sslContext
(
sslContext
)
.
register
(
JacksonJsonProvider
.
class
)
.
build
();
We create a
ClientBuilder
instance by calling the static method
ClientBuild-
er.newBuilder()
. We then set a proprietary, JAX-RS implementation-specific configura-
tion property that controls socket connection timeouts. Next we specify the
sslContext
we
want to use to manage
HTTPS
connections. The RESTful services we're going to interact
with are primarily JSON, so we
register()
an
@Provider
that knows how to marshal Java
objects to and from JSON. Finally, we call
build()
to create the
Client
instance.
WARNING
Always remember to
close()
your
Client
objects.
Client
objects often pool connections for per-
formance reasons. If you do not close them, you are leaking valuable system resources. While most
JAX-RS implementations implement a
finalize()
method for
Client
, it is not a good idea to rely
on the garbage collector to clean up poorly written code.
Client and WebTarget
Now that we have a
Client
, there's a bunch of stuff we can do with this object. Like
Cli-
entBuilder
, the
Client
interface implements
Configurable
. This allows you to change
configuration and register components for the
Client
on the fly as your application ex-
ecutes. The most important purpose of
Client
, though, is to create
WebTarget
instances:
package
package
javax
.
ws
.
rs
.
client
.
Client
;
public
public interface
interface
Client
Client
extends
extends
Configurable
<
Client
> {
public
public
void
void
close
();
public
public
WebTarget
target
(
String uri
);
public
public
WebTarget
target
(
URI uri
);
public
public
WebTarget
target
(
UriBuilder uriBuilder
);
public
public
WebTarget
target
(
Link link
);