Resteasy Client API
2017-02-14 16:05
1616 查看
1.JAX-RS 2.0 Client API
JAX-RS 2.0介绍了一种新的client API,涉及主要3个类:Client , WebTarget 和Response。有两种方式来创建一个Client,标准的方式和使用ResteasyClientBuilder类,后一种方式配置client的方法更少。
Resteasy会自动加载一组默认的providers,(默认所有的类都在META-INF/services/javax.ws.rs.ext.Providers文件)。另外通过调用Client.configuration得到Configuration类,也可以注册其它的provider, filters和interceptors。
2.Reaseasy Proxy Framework
Resteasy 代理框架和JAX-RS服务端相反,它不是在使用JAX-RS 注解来匹配访问RESTFul web服务方法的请求,而是创建一个HTTP请求访问一个可以不用JAX-RS注解的RESTFulweb服务。
或者
3.Apache HTTP Client 4.x and other backends
Resteasy中,在客户端和服务端之间网络通信默认由HttpClient操作。Resteasy 客户端框架和网络间接口为org.jboss.resteasy.client.jaxrs.ClientHttpEngine和,它的一个实现类为org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine,使用HttpClient(4.x),在org.jboss.resteasy.client.jaxrs.engines包下还有其它的实现类:
(1)URLConnectionClientExecutor: 使用java.net.HttpURLCConnection
(2)InMemoryClientExecutor:分发请求到同一个JVM中的主机。
ApacheHttpClient4Engine可以使用org.apache.http.client.HttpClient和org.apache.http.protocol.HttpContext来定制。
JAX-RS 2.0介绍了一种新的client API,涉及主要3个类:Client , WebTarget 和Response。有两种方式来创建一个Client,标准的方式和使用ResteasyClientBuilder类,后一种方式配置client的方法更少。
Client client = ClientBuilder.newClient(); ... or... Client client = ClientBuilder.newBuilder().build(); WebTarget target = client.target("http://foo.com/resource"); Response response = target.request().get(); String value = response.readEntity(String.class); response.close(); // You should close connections! ResteasyClient client = new ResteasyClientBuilder().build(); ResteasyWebTarget target = client.target("http://foo.com/resource");
Resteasy会自动加载一组默认的providers,(默认所有的类都在META-INF/services/javax.ws.rs.ext.Providers文件)。另外通过调用Client.configuration得到Configuration类,也可以注册其它的provider, filters和interceptors。
2.Reaseasy Proxy Framework
Resteasy 代理框架和JAX-RS服务端相反,它不是在使用JAX-RS 注解来匹配访问RESTFul web服务方法的请求,而是创建一个HTTP请求访问一个可以不用JAX-RS注解的RESTFulweb服务。
public interface SimpleClient { @GET @Path("basic") @Produces("text/plain") String getBasic(); @PUT @Path("basic") @Consumes("text/plain") void putBasic(String body); @GET @Path("queryParam") @Produces("text/plain") String getQueryParam(@QueryParam("param")String param); @GET @Path("matrixParam") @Produces("text/plain") String getMatrixParam(@MatrixParam("param")String param); @GET @Path("uriParam/{param}") @Produces("text/plain") int getUriParam(@PathParam("param")int param); }
Client client = ClientFactory.newClient(); WebTarget target = client.target("http://example.com/base/uri"); ResteasyWebTarget rtarget = (ResteasyWebTarget)target; SimpleClient simple = rtarget.proxy(SimpleClient.class); client.putBasic("hello world");
或者
ResteasyClient client = new ResteasyClientBuilder().build(); ResteasyWebTarget target = client.target("http://example.com/base/uri"); SimpleClient simple = target.proxy(SimpleClient.class); client.putBasic("hello world");
3.Apache HTTP Client 4.x and other backends
Resteasy中,在客户端和服务端之间网络通信默认由HttpClient操作。Resteasy 客户端框架和网络间接口为org.jboss.resteasy.client.jaxrs.ClientHttpEngine和,它的一个实现类为org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine,使用HttpClient(4.x),在org.jboss.resteasy.client.jaxrs.engines包下还有其它的实现类:
(1)URLConnectionClientExecutor: 使用java.net.HttpURLCConnection
(2)InMemoryClientExecutor:分发请求到同一个JVM中的主机。
ApacheHttpClient4Engine可以使用org.apache.http.client.HttpClient和org.apache.http.protocol.HttpContext来定制。
// Configure HttpClient to authenticate preemptively // by prepopulating the authentication data cache. // 1. Create AuthCache instance AuthCache authCache = new BasicAuthCache(); // 2. Generate BASIC scheme object and add it to the local auth cache BasicScheme basicAuth = new BasicScheme(); authCache.put("com.bluemonkeydiamond.sippycups", basicAuth); // 3. Add AuthCache to the execution context BasicHttpContext localContext = new BasicHttpContext(); localContext.setAttribute(ClientContext.AUTH_CACHE, authCache); // 4. Create client executor and proxy httpClient = new DefaultHttpClient(); ApacheHttpClient4Engine engine = new ApacheHttpClient4Engine(httpClient, localContext); ResteasyClient client = new RestasyClientBuilder().httpEngine(engine).build();
相关文章推荐
- Forget Restclient and Embrace Postman for Rest API testing
- Springmvc+ResteasyClient 整合无法Post对象的问题
- 如何使用Wisdom RESTClient定制满足您个性化需求的API文档?
- jboss resteasy client ProxyFactory 自动化测试架构
- chrome http请求,测试webapp接口之DHC - REST/HTTP API Client
- JIRA Rest JAVA Client API实现问题管理及自定义字段(原创)
- JIRA Rest JAVA Client API实现问题管理及自定义字段(原创)
- ReactiveCocoa实战: 模仿 "花瓣",重写 LeanCloud Rest Api的iOS REST Client.
- Chrome Restful Api 测试工具 Postman-REST-Client离线安装包下载,Axure RP Extension for Chrome离线版下载
- RESTful API文档生成工具Wisdom RESTClient
- RESTEasy通过ApacheAPI或JavaAPI进行TEXT、XML、JSON数据交换
- ReactiveCocoa实战: 模仿 "花瓣",重写 LeanCloud Rest Api的iOS REST Client.
- REST Client组件例子,及获取数据api的一些知识
- Java - 在WebService中使用Client调用三方的RestAPI
- RESTful API 文档生成神器Wisdom RESTClient
- Elasticsearch Java Rest Client API 整理总结 (一)
- chrom插件DHC - REST/HTTP API Client使用教程
- 提供一个wink restclient用get访问restapi的例子(java语言)
- Wisdom RESTClient支持自动化测试并可以生成API文档
- 如何使用Wisdom RESTClient定制满足您个性化需求的API文档?