javax.ws.rs.client.Client 如何配置 readTimeOut?

2022-09-03 05:47:19

com.sun.jersey.api.client.Clientjavax.ws.rs.client.Client.Client 如何配置 Client?

从:

import com.sun.jersey.api.client.Client;

Client client = Client.create();
client.setReadTimeout(1000 * 60 * 20);
client.setConnectTimeout(1000 * 20);
webResource = client.resource("someWhereOverTheRainbow");
..etc.

自:

import javax.ws.rs.client.*;

Client client = ClientBuilder.newClient();
// **now what?** client.getConfiguration().getProperties().put("isThisTheWayToDoIt", 1000 * 60 * 2);

WebTarget target = client.target("someWhereOverTheRainbow");
..etc.

我正在使用 javax.ws.rs-api-2.0.jar


答案 1

我假设你正在使用jax-rs-ri。为此,您可以使用ClientProperties.CONNECT_TIMEOUTClientProperties.READ_TIMEOUT

例:

ClientConfig configuration = new ClientConfig();
configuration = configuration.property(ClientProperties.CONNECT_TIMEOUT, 1000);
configuration = configuration.property(ClientProperties.READ_TIMEOUT, 1000);
Client client = ClientBuilder.newClient(configuration);
WebTarget target = client.target(
        "http://developer.github.com/v3/");
String content = target.request().get(String.class);
System.out.println(content);

编辑:

我阅读了 ClientConfig.property 的 API 文档。@Gili是对的。


答案 2