您的位置:首页 > 其它

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的方法更少。

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();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: