Package com.sun.jersey.api.client

Provides support for client-side communication with HTTP-based RESTful Web services.

See: Description

Package com.sun.jersey.api.client Description

Provides support for client-side communication with HTTP-based RESTful Web services.

The client API is high-level API that reuses many aspects of the JAX-RS API. It is designed to be quick, easy to use and is especially useful and productive when developing tests for Web services.

The client API can be used as follows to make simple GET and POST requests to a Web resource:

     Client c = Client.create();
     WebResource r = c.resource("http://host/base");
     String s = r.get(String.class);
     s = r.post(String.class, s);
 

A key concept of REST is the uniform interface and this is encapsulated in the WebResource class, which implements UniformInterface. A request is built up and when the corresponding HTTP method on the UniformInterface is invoked the request is sent to the Web resource and the returned response is processed. This enables efficient production of requests as follows:

     WebResource r = ...
     String s = r.accept("application/xml").get(String.class);
     s = r.accept("application/xml").type("application/xml").post(String.class, s);
 
In the above example a GET request occurs stating that the "application/xml" is acceptable. After that a POST request occurs stating the same acceptable media type and that the content type of the request entity is "application/xml".

The Java types that may be used for request and response entities are not restricted to String. The client API reuses the same infrastrucure as JAX-RS server-side. Thus the same types that can be used on the server-side can also be used on the client-side, such as JAXB-based types. Further more the supported Java types can be extended by implementing MessageBodyReader and MessageBodyWriter. For registration of such support for new Java types see ClientConfig.

A type of ClientResponse declared for the response entity may be used to obtain the status, headers and response entity.

If any type, other than ClientResponse, is declared and the response status is greater than or equal to 300 then a UniformInterfaceException exception will be thrown, from which the ClientResponse instance can be accessed.

In the following cases it is necessary to close the response, when response processing has completed, to ensure that underlying resources are correctly released.

If a response entity is declared of the type ClientResponse or of a type that is assignable to Closeable (such as InputStream) then the response must be either: 1) closed by invoking the method ClientResponse.close() or Closeable.close(); or 2) all bytes of response entity must be read.

If a UniformInterfaceException is thrown then by default the response entity is automatically buffered and the underlying resources are correctly released. See the following property for more details: ClientConfig.PROPERTY_BUFFER_RESPONSE_ENTITY_ON_EXCEPTION.

Copyright © 2016 Oracle Corporation. All Rights Reserved.