标签:
Since a resource is represented as a Java type it makes it easy to configure, pass around and inject in ways that is not so intuitive or possible with other client-side APIs.
The Jersey Client API reuses many aspects of the JAX-RS and the Jersey implementation such as:
byte[]
, String
, InputStream
, File
, DataSource
and JAXB beans in addition to Jersey specific features such as JSON support and MIME Multipart support.Some APIs, like the Apache HTTP client or java.net.HttpURLConnection, can be rather hard to use and/or require too much code to do something relatively simple.
This is why the Jersey Client API provides support for wrapping HttpURLConnection and the Apache HTTP client. Thus it is possible to get the benefits of the established implementations and features while getting the ease of use benefit.
It is not intuitive to send a POST request with form parameters and receive a response as a JAXB object with such an API. For example with the Jersey API this is very easy:
Form f = new Form(); f.add("x", "foo"); f.add("y", "bar"); Client c = Client.create(); WebResource r = c.resource("http://localhost:8080/form"); JAXBBean bean = r.type(MediaType.APPLICATION_FORM_URLENCODED_TYPE) .accept(MediaType.APPLICATION_JSON_TYPE) .post(JAXBBean.class, f);
In the above code a Form is created with two parameters, a new WebResource instance is created from a Client then the Form
instance is POST
ed to the resource, identified with the form media type, and the response is requested as an instance of a JAXB bean with an acceptable media type identifying the Java Script Object Notation (JSON) format. The Jersey client API manages the serialization of the Form
instance to produce the request and de-serialization of the response to consume as an instance of a JAXB bean.
If the code above was written using HttpURLConnection
then the developer would have to write code to serialize the form sent in the POST request and de-serialize the response to the JAXB bean. In addition further code would have to be written to make it easy to reuse the same resource “http://localhost:8080/form” that is encapsulated in the WebResource
type.
Jersey(1.19.1) - Ease of use and reusing JAX-RS artifacts
标签:
原文地址:http://www.cnblogs.com/huey/p/5400919.html