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:
Client c = Client.create();
Configuring a Client and WebResource
The client instance can then be configured by setting properties on the map returned from the getProperties methods or by calling the specific setter methods, for example the following configures the client to perform automatic redirection for appropriate responses:
c.getProperties().put(ClientConfig.PROPERTY_FOLLOW_REDIRECTS, true);
which is equivalent to the following:
c.setFollowRedirects(true);
Alternatively it is possible to create a Client instance using a ClientConfig object for example:
ClientConfig cc = new DefaultClientConfig();
cc.getProperties().put(ClientConfig.PROPERTY_FOLLOW_REDIRECTS, true);
Client c = Client.create(cc);
Once a client instance is created and configured it is then possible to obtain a WebResource instance, which will inherit the configuration declared on the client instance. For example, the following creates a reference to a Web resource with the URI “http://localhost:8080/xyz”:
WebResource r = c.resource("http://localhost:8080/xyz");
and redirection will be configured for responses to requests invoked on the Web resource.
Client instances are expensive resources. It is recommended a configured instance is reused for the creation of Web resources. The creation of Web resources, the building of requests and receiving of responses are guaranteed to be thread safe. Thus a Client instance and WebResource instances may be shared between multiple threads.
In the above cases a WebResource instance will utilize HttpUrlConnection or HttpsUrlConnection, if the URI scheme of the WebResource is “http” or “https” respectively.
Building a request
Requests to a Web resource are built using the builder pattern (see RequestBuilder) where the terminating method corresponds to an HTTP method (see UniformInterface). For example,
String response = r.accept(MediaType.APPLICATION_JSON_TYPE, MediaType.APPLICATION_XML_TYPE)
.header("X-FOO", "BAR")
.get(String.class);
The above sends a GET request with an Accept header of application/json, application/xml and a non-standard header X-FOO of BAR.
If the request has a request entity (or representation) then an instance of a Java type can be declared in the terminating HTTP method, for PUT, POST and DELETE requests. For example, the following sends a POST request:
String request = "content";
String response = r.accept(MediaType.APPLICATION_JSON_TYPE, MediaType.APPLICATION_XML_TYPE)
.header("X-FOO", "BAR")
.post(String.class, request);
where the String "content" will be serialized as the request entity (see the section "Java instances and types for representations" section for further details on the supported Java types). The Content-Type of the request entity may be declared using the type builder method as follows:
String response = r.accept(MediaType.APPLICATION_JSON_TYPE, MediaType.APPLICATION_XML_TYPE)
.header("X-FOO", "BAR")
.type(MediaType.TEXT_PLAIN_TYPE)
.post(String.class, request);
or alternatively the request entity and type may be declared using the entity method as follows:
String response = r.accept(MediaType.APPLICATION_JSON_TYPE, MediaType.APPLICATION_XML_TYPE)
.header("X-FOO", "BAR")
.entity(request, MediaType.TEXT_PLAIN_TYPE)
.post(String.class);
Receiving a response
If the response has a entity (or representation) then the Java type of the instance required is declared in the terminating HTTP method. In the above examples a response entity is expected and an instance of String is requested. The response entity will be de-serialized to a String instance.
If response meta-data is required then the Java type ClientResponse can be declared from which the response status, headers and entity may be obtained. For example, the following gets both the entity tag and response entity from the response:
ClientResponse response = r.get(ClientResponse.class);
EntityTag e = response.getEntityTag();
String entity = response.getEntity(String.class);
If the ClientResponse type is not utilized and the response status is greater than or equal to 300 then the runtime exception UniformInterfaceException is thrown. This exception may be caught and the ClientResponse obtained as follows:
try {
String entity = r.get(String.class);
} catch (UniformInterfaceException ue) {
ClientResponse response = ue.getResponse();
}
Creating new WebResources from a WebResource
A new WebResource can be created from an existing WebResource by building from the latter's URI. Thus it is possible to build the request URI before building the request. For example, the following appends a new path segment and adds some query parameters:
WebResource r = c.resource("http://localhost:8080/xyz");
MultivaluedMap<String, String> params = new MultivaluedMapImpl();
params.add("foo", "x");
params.add("bar", "y");
String response = r.path("abc")
.queryParams(params)
.get(String.class);
that results in a GET request to the URI "http://localhost:8080/xyz/abc?foo=x&bar=y".
Java instances and types for representations
All the Java types for representations supported by the Jersey server side for requests and responses are also supported on the client side. This includes the standard Java types as specified by JAX-RS in addition to JSON, Atom and Multipart MIME as supported by Jersey.
To process a response entity (or representation) as a stream of bytes use InputStream as follows:
InputStream in = r.get(InputStream.class);
// Read from the stream
in.close();
Note that it is important to close the stream after processing so that resources are freed up.
To POST a file use File as follows:
File f = ...
String response = r.post(String.class, f);
Refer to the JAXB sample to see how JAXB with XML and JSON can be utilized with the client API (more specifically, see the unit tests).
Jersey(1.19.1) - Client API, Overview of the API的更多相关文章
- 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 ...
- Jersey(1.19.1) - Client API, Ease of use and reusing JAX-RS artifacts
Since a resource is represented as a Java type it makes it easy to configure, pass around and inject ...
- Jersey(1.19.1) - Client API, Using filters
Filtering requests and responses can provide useful functionality that is hidden from the applicatio ...
- 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 ...
- 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 ...
- Jersey(1.19.1) - Client API, Proxy Configuration
为 Jersey Client 设置代理,可以使用带有 ClientHandler 参数的构造方法创建 Client 实例. public static void main(String[] args ...
- Blobstore Java API overview
Blobstore API允许你的应用程序使用(serve)叫做Blobs的数据对象.这种数据对象比Datastore服务所允许的对象的尺寸大得多.Blobs能有效地为大文件比如视频.图片提供服务,允 ...
- 使用 WSO2 API Manager 管理 Rest API
WSO2 API Manager 简介 随着软件工程的增多,越来越多的软件提供各种不同格式.不同定义的 Rest API 作为资源共享,而由于这些 API 资源的异构性,很难对其进行复用.WSO2 A ...
- Atitit.一个cms有多少少扩展点,多少api wordpress cms有多少api。。扩展点
Atitit.一个cms有多少少扩展点,多少api wordpress cms有多少api..扩展点 1. Api分类 WordPress APIs1 1.1. 1 函数分类2 1.2. 函数api ...
随机推荐
- 高扩展的基于NIO的服务器架构
当你考虑写一个扩展性良好的基于Java的服务器时,相信你会毫不犹豫地使用Java的NIO包.为了确保你的服务器能够健壮.稳定地运行,你可能会花大量的时间阅读博客和教程来了解线程同步的NIO selec ...
- 为什么要坚持用ASP.NET MVC!(②)
尽管ASP.NET MVC架构和Web Form架构区别很大,但是还是有很多共同之处.毕竟它们都是以ASP.NET API与.NET框架为基础构建的.比较一下ASP.NET MVC和Web Form框 ...
- kaptcha验证码插件的使用
kaptcha 是一个非常实用的验证码生成工具.有了它,你可以生成各种样式的验证码,因为它是可配置的.kaptcha工作的原理是调用 com.google.code.kaptcha.servlet.K ...
- Cocos2d-x 让精灵随手指移动起来二(简单实现)
void HelloWorld::ccTouchMoved(cocos2d::CCTouch *touch, cocos2d::CCEvent *event) { CCSize winSize = C ...
- C++,C#,Python
1.C++的思路:无论是基本类型,还是类类型,对象的传递提供了两种方式,一个是整体拷贝,一个是复制引用.整体拷贝对应着copy构造和copy赋值,复制引用就是通过引用或者指针实现的,当然指针本身还是整 ...
- POJ 3522 Slim Span 最小差值生成树
Slim Span Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=3522 Description Gi ...
- BZOJ 3505: [Cqoi2014]数三角形 数学
3505: [Cqoi2014]数三角形 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/pr ...
- Html&CSS 今日心得
今天和秋秋一起review了一下我自己写的登录页面.她给我提了几个point,对我很有启发. css样式的代码和html代码分离. 我自己做的时候是在google console里面调好了样式以后就直 ...
- 开发者必备,超实用的PHP代码片段(转)
此前,研发频道曾发布<直接拿来用,10个PHP代码片段>,得到了网友们的一致好评.本文,笔者将继续分享九个超级有用的PHP代码片段.当你在开发网站.应用或者博客时,利用这些代码能为你节省大 ...
- php执行效率相关的语句
一:字符替换: strtr > str_replace > preg_replace 注意: 1:一般用strtr函数的这种形式:string strtr ( string $str , ...