plain HTTP communication API as well as integration with the JAX-RS extension
providers, in order to enable concise and efficient implementation of
reusable client-side solutions that leverage existing and well
established client-side implementations of HTTP-based communication.
The JAX-RS Client API encapsulates the Uniform Interface Constraint – a key constraint of the REST architectural style – and associated data elements as client-side Java artifacts and supports a pluggable architecture by defining multiple extension points.
javax.ws.rs.client.ClientBuilder
that is used to bootstrap javax.ws.rs.client.Client instances -
configurable, heavy-weight objects
that manage the underlying communication infrastructure and serve as the root
objects for accessing any Web resource. The following example illustrates the
bootstrapping and configuration of a Client instance:
Client client = ClientBuilder.newClient();
client.property("MyProperty", "MyValue")
.register(MyProvider.class)
.register(MyFeature.class);
text/plain representation of the resource identified by
"http://example.org/hello":
Client client = ClientBuilder.newClient();
Response res = client.target("http://example.org/hello").request("text/plain").get();
Conceptually, the steps required to submit a request are the following:
javax.ws.rs.client.Client instanceWebTarget pointing at a Web resourcebuild a requestjavax.ws.rs.client.Invocation for later submissionWebTarget instance is bound to a
concrete URI, e.g. "http://example.org/messages/123",
or a URI template, e.g. "http://example.org/messages/{id}".
That way a single target can either point at a particular resource or represent
a larger group of resources (that e.g. share a common configuration) from which
concrete resources can be later derived:
// Parent target for all messages
WebTarget messages = client.target("http://example.org/messages/{id}");
// New target for http://example.org/messages/123
WebTarget msg123 = messages.resolveTemplate("id", 123);
// New target for http://example.org/messages/456
WebTarget msg456 = messages.resolveTemplate("id", 456);
javax.ws.rs.client.Invocation is a request that has been prepared
and is ready for execution.
Invocations provide a generic interface that enables a separation of concerns
between the creator and the submitter. In particular, the submitter does not
need to know how the invocation was prepared, but only whether it should be
executed synchronously or asynchronously.
Invocation inv1 = client.target("http://example.org/atm/balance")
.queryParam("card", "111122223333").queryParam("pin", "9876")
.request("text/plain").buildGet();
Invocation inv2 = client.target("http://example.org/atm/withdrawal")
.queryParam("card", "111122223333").queryParam("pin", "9876")
.request().buildPost(text("50.0")));
Collection invs = Arrays.asList(inv1, inv2);
// Executed by the submitter
Collection ress = Collections.transform(invs, new F() {
public Response apply(Invocation inv) {return inv.invoke(); }
});
| Modifier and Type | Interface and Description |
|---|---|
| public interface | AsyncInvoker
Uniform interface for asynchronous invocation of HTTP methods. |
| public interface | Client
Client is the main entry point to the fluent API used to build and execute client requests in order to consume responses returned. |
| public interface | ClientRequestContext
Client request filter context. |
| public interface | ClientRequestFilter
An extension interface implemented by client request filters. |
| public interface | ClientResponseContext
Client response filter context. |
| public interface | ClientResponseFilter
An extension interface implemented by client response filters. |
| public interface | CompletionStageRxInvoker
Reactive invoker based |
| public interface | Invocation
A client request invocation. |
| public interface | InvocationCallback<
response type. It can be either a general-purpose
RESPONSE>javax.ws.rs.core.Response or the anticipated response entity
type.Callback that can be implemented to receive the asynchronous processing events from the invocation processing. |
| public interface | RxInvoker<
a type representing the asynchronous computation. T>Uniform interface for reactive invocation of HTTP methods. |
| public interface | |
| public interface | SyncInvoker
Uniform interface for synchronous invocation of HTTP methods. |
| public interface | WebTarget
A resource target identified by the resource URI. |
| Modifier and Type | Class and Description |
|---|---|
| public abstract class | ClientBuilder
Main entry point to the client API used to bootstrap |
| public class | |
| pack-priv class | FactoryFinder
Factory finder utility class. |
| public class | ResponseProcessingException
JAX-RS client-side runtime processing exception thrown to indicate that response processing has failed (e.g. in a filter chain or during message entity de-serialization). |