码迷,mamicode.com
首页 > 其他好文 > 详细

Jersey(1.19.1) - Ease of use and reusing JAX-RS artifacts

时间:2016-04-17 14:44:25      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:

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:

  1. URI building using UriBuilder and UriTemplate to safely build URIs;
  2. Support for Java types of representations such as byte[]StringInputStreamFileDataSource and JAXB beans in addition to Jersey specific features such as JSON support and MIME Multipart support.
  3. Using the builder pattern to make it easier to construct requests.

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 POSTed 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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!