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) - Client API, Ease of use and reusing JAX-RS artifacts的更多相关文章

  1. Jersey(1.19.1) - Client API, Uniform Interface Constraint

    The Jersey client API is a high-level Java based API for interoperating with RESTful Web services. I ...

  2. Jersey(1.19.1) - Client API, Overview of the API

    To utilize the client API it is first necessary to create an instance of a Client, for example: Clie ...

  3. Jersey(1.19.1) - Client API, Using filters

    Filtering requests and responses can provide useful functionality that is hidden from the applicatio ...

  4. Jersey(1.19.1) - Client API, Testing services

    The Jersey client API was originally developed to aid the testing of the Jersey server-side, primari ...

  5. Jersey(1.19.1) - Client API, Security with Http(s)URLConnection

    With Http(s)URLConnection The support for security, specifically HTTP authentication and/or cookie m ...

  6. Jersey(1.19.1) - Client API, Proxy Configuration

    为 Jersey Client 设置代理,可以使用带有 ClientHandler 参数的构造方法创建 Client 实例. public static void main(String[] args ...

  7. docker报Error response from daemon: client is newer than server (client API version: 1.24, server API version: 1.19)

    docker version Client: Version: 17.05.0-ce API version: 1.24 (downgraded from 1.29) Go version: go1. ...

  8. Jersey(1.19.1) - JSON Support

    Jersey JSON support comes as a set of JAX-RS MessageBodyReader<T> and MessageBodyWriter<T&g ...

  9. Jersey(1.19.1) - Hello World, Get started with Jersey using the embedded Grizzly server

    Maven Dependencies The following Maven dependencies need to be added to the pom: <dependency> ...

随机推荐

  1. OOP 6大基本原则

    1.开闭原则: 对扩展开发.对修改关闭. 2.里氏替换原则:子类替换父类(可以用父类对象的任何地方都可以用子类对象代替) 3.依赖倒置原则:程序要依赖于抽象接口,不要依赖于具体实现.简单的说就是要求对 ...

  2. Unity3D之Mecanim动画系统学习笔记(二):模型导入

    我们要在Unity3D中使用上模型和动画,需要经过下面几个阶段的制作,下面以一个人形的模型开发为准来介绍. 模型制作 模型建模(Modelling) 我们的美术在建模时一般会制作一个称为T-Pose( ...

  3. ORACLE R12 MOAC

    MOAC简介 MOAC(Multi-Org Access Control)为多组织访问控制,是Oracle EBS R12的重要新功能.它可以实现在一个Responsibility下对多个OU(Ope ...

  4. SQL 表锁(转)

    其实你可以使用事务处理   比方说在一个字段里面添加一个boolean 的字段当你要处理该字段的时候就 True 哪么别的人都不可以进行操作 如果是False 哪么就可以进行操作--呵可--我是这样的 ...

  5. CSS 布局总结——变宽度布局

    变宽度布局 1-2-1 等比例变宽 总宽度设置 width: 85%; min-width: 650px; (关于IE6的min-width支持,可用) content 设置 width: 66%;  ...

  6. 【转】linux中的常见目录及文件

    1. /proc目录 Linux 内核提供了一种通过 /proc 文件系统,在运行时访问内核内部数据结构.改变内核设置的机制.proc文件系统是一个伪文件系统,它只存在内存当中,而不占用外存空间.它以 ...

  7. Codeforces Round #332 (Div. 2) B. Spongebob and Joke 水题

    B. Spongebob and Joke Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/599 ...

  8. Codeforces gym 100685 E. Epic Fail of a Genie 贪心

    E. Epic Fail of a GenieTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100685 ...

  9. 三分钟掌握 JUnit3.0

    曾经公司做过一个.net的项目,在项目开发的过程中.我们採用的是分层的开发方式,大家先在一起讨论接口, 然后讨论完以后,形成文档,然后依照文档进行开发!这样就有一个问题,你必需要保证你的接口是正确的. ...

  10. 【工作记录】android手势事件操作记录

    /* 用户按下触摸屏.快速移动后松开 public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float vel ...