jersey -rs client使用

来源:互联网 发布:天涯全自动营销软件 编辑:程序博客网 时间:2024/06/05 09:50

基本

通过使用 WebResource 对象来创建要发送到 Web 资源的请求,以及处理从 Web 资源返回的响应。例如,你可以使用 WebResource 对象来发送 HTTP GET、PUT、POST 以及 DELETE 请求。



    GET 请求 :使用 WebResource 类的 get() 方法来提交一个 HTTP GET请求到 Web 资源:
String s = webResource.get(String.class);
    这表示如果 WebResource 对象的 URL 是 http://example.com/base,那么一个 HTTP GET 请求将会发送到地址为 http://example.com/base 的资源。如果你熟悉命令行下的 HTTP 工具 curl,那么你可以知道:
String s = webResource.get(String.class);
    相应的 curl 命令如下:
curl http://example.com/base
    你还可以指定 get() 请求时的查询参数。例如,下面的代码在 get() 请求中指定了两个查询参数:
MultivaluedMap queryParams = new MultivaluedMapImpl();
queryParams.add("param1", "val1");
queryParams.add("param2", "val2");
String s = webResouce.queryParams(queryParams).get(String.class);
    相应的 curl 命令如下:
curl http://example.com/base?param1=val1&param2=val2
    你还可以指定响应所能接受的 MIME 类型。例如,下面的代码指定了响应的 MIME 类型只能为文本:
String s = webResource.accept("text/plain").get(String.class);
    相应的 curl 命令如下:
curl -HAccept:text/plain http://example.com/base
    另外,你还可以获取对应请求的 HTTP 状态码,例如下面这个例子展示获取一个请求所返回的文本实体与状态码:   
ClientResponse response = webResource.accept("text/plain")


                                     .get(ClientResponse.class);


int status = response.getStatus();


String textEntity = response.getEntity(String.class);  
    ClientResponse 对象代表了一个客户端收到的 HTTP 响应。
 
    PUT 请求 :使用 WebResource 类的 put() 方法来提交一个 HTTP PUT 请求到 Web 资源。例如下面的代码展示了请求发送一个文本实体 foo:bar 到指定的 Web 资源:
ClientResponse response = webResource.type("text/plain")


                                     .put(ClientResponse.class, "foo:bar");
    相应的 curl 命令如下:
curl -XPUT -HContent-type:text/plain --data "foo:bar" http://example.com/base
    同样,你也可以在使用 put() 方法发送请求时指定查询参数,方法与使用 get() 方法时指定查询参数一样。在下面的例子中,把在之前 get() 方法示例中使用过的两个同样的查询参数指定到了一个 put() 请求中:
MultivaluedMap queryParams = new MultivaluedMapImpl();


queryParams.add("param1", "val1");


queryParams.add("param2", "val2");


ClientResponse response = webResource.queryParams(queryParams)


                                     .put(ClientResponse.class, "foo:bar");


    相应的 curl 命令如下:
curl -XPUT -HContent-type:text/plain --data "foo:bar" http://example.com/base?param1=val1&param2=val2


    POST 请求 :一个 POST 请求相当于一个 GET 请求和一个 PUT 请求的综合,也就意味着,你可以使用 POST 请求来发送一个实体到指定的 Web 资源并且接收另一个实体。使用 WebResource 类的 post() 方法来发送一个 HTTP POST 请求到指定的 Web 资源。下面的例子展示了发送一个带有查询参数以及进行了 URL 编码的表单数据的 POST 请求:
MultivaluedMap formData = new MultivaluedMapImpl();


formData.add("name1", "val1");


formData.add("name2", "val2");


ClientResponse response = webResource.type("application/x-www-form-urlencoded")


                                     .post(ClientResponse.class, formData);


    相应的 curl 命令如下:
curl -d name1=val1 -d name2=val2 http://example.com/base


    DELETE 请求:使用 Web Resource 类的 delete() 方法来发送珍上 HTTP DELETE 请求到指定的 Web 资源。例如,下面的例子展示删除一个 URI 为 http://example.com/base/user/123 资源:
ClientResponse response = webResource.path("user/123")


                                     .delete(ClientResponse.class);


    相应的 curl 命令如下:
curl -XDELETE http://example.com/base/user/123


    另外,Web Resource.path() 方法可以在所有 HTTP 请求中使用,它可以让你给要请求的 Web 资源指定一个额外的路径。另一个 WebResouce 类的方法 header() 可以给你的请求添加 HTTP 头部信息。

配置:

    ClientConfig config = new DefaultClientConfig();


    config.getClasses().add(JSONRootElementProvider.class);


    Client client = Client.create(config);

过滤器:

    import com.sun.jersey


    .api.client.filter.LoggingFilter


    client.addFilter(new LoggingFilter());

使用SSL:

    ClientConfig config = new DefaultClientConfig();


    SSLContext ctx = SSLContext.getInstance("SSL");


    ctx.init(null, myTrustManager, null);


    config.getProperties().put(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES,


                             new HTTPSProperties(hostnameVerifier, ctx));


    Client client = Client.create(config);



0 0
原创粉丝点击