将 Jersey 1.x 客户端转换为 Jersey 2.x 客户端

Man*_*tra 1 java jersey jersey-client jersey-1.0 jersey-2.0

我有以下适用于 jersey 1.x 的代码。然而,我需要让它与 jersey 2.x 一起工作,并且我注意到很多 jersey 类和方法从一个版本更改为另一个版本。任何想法?

Client client = null;

try {
    URLConnectionClientHandler ch = new URLConnectionClientHandler(new ProxyConnectionFactory(proxyHost, proxyPort));
    client = new Client(ch);
    WebResource webResource = client.resource(url);
    ClientResponse response = ((Builder) webResource.type("application/json").header(authKey, authCreds)).post(ClientResponse.class, input);
    
    String output = (String) response.getEntity(String.class);
    System.out.println(output);
    if (response.getStatus() != 200) {
        System.out.println("Status Failed, Status: " + response.getStatus());
    }
    else {
        System.out.println("Connection Successful!");
        //additional code
    }
    
} catch (Exception e) {
    System.out.println("Exception occurred");
} finally {
    client.destroy();
}
Run Code Online (Sandbox Code Playgroud)

在此代码片段中,ProxyConnectionFactory 是一个设置代理配置的类。它实现了 HttpURLConnectionFactory,这也是 jersey 1.x 接口。

Pau*_*tha 5

基本类型类似物如下:

泽西岛 1.x 泽西岛 2.x
Client javax.ws.rs.client.Client
WebResource javax.ws.rs.client.WebTarget
ClientResponse javax.ws.rs.core.Response

要构建客户端,通常使用ClientBuilder静态构建器方法之一。使用的最基本的方法是该newClient()方法,它返回一个新的实例Client

Client client = ClientBuider.newClient();
Run Code Online (Sandbox Code Playgroud)

如果您需要配置客户端,可以通过多种方式进行。例如,如果您需要注册一些属性或提供者,您可以:

一旦你拥有了Client,你就想得到一个WebTarget。您可以通过向该方法提供 URI 来实现这一点Client#target()

Client client = ClientBuilder.newBuilder()
        .property("...", "...")
        .register(SomeProvider.class)
        .build();
Run Code Online (Sandbox Code Playgroud)

如果您需要对 URI 执行任何操作,例如添加路径、查询参数或矩阵参数,您可以在WebTarget. 否则,您现在将调用该WebTarget#request()方法来获取Invocation.Builder

ClientConfig config = new ClientConfig()
        .property("...", "...")
        .register(SomeProvider.class);
Client client = ClientBuilder.newClient(config);
Run Code Online (Sandbox Code Playgroud)

使用Invocation.Builder,您可以添加标头并最终发出请求。您不必为 分配任何新变量Invocation.Builder(甚至不必WebTarget为 分配任何新变量)。我这样做是出于演示目的。您可以继续链接方法调用。例如

Client client = ClientBuilder.newClient();
client.property("...", "...");
client.register(SomeProvider.class);
Run Code Online (Sandbox Code Playgroud)

最后,要发出请求,您将使用Invocation.Builder. 在你的情况下,这就是post()方法。您可以将 an 传递Entity给此方法,结果将是Response

Client client = ClientBuilder.newClient();
WebTarget target = client.target(uri);
Run Code Online (Sandbox Code Playgroud)

要阅读响应,您可以使用Response#readEntity(Class)

Client client = ClientBuilder.newClient();
WebTarget target = client.target(uri);
Invocation.Builder invBuilder = target.request();
Run Code Online (Sandbox Code Playgroud)

如果您有一个 POJO 类,您希望将响应反序列化,然后将该类传递给方法readEntity()。无论您需要什么数据类型,您都需要一个提供者。例如,如果要将 JSON 转换为 POJO,那么您将需要 Jackson 提供程序:

Client client = ClientBuilder.newClient();
client.target(url)
        .request()
        .header(authHey, authCreds)
Run Code Online (Sandbox Code Playgroud)

代理

就代理而言,泽西岛有一些您可以设置的属性

也可以看看

  • 有关 Jersey 2.x 客户端的完整文档,请参阅文档