16

com.sun.jersey.api.client.Clientjavax.ws.rs.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

4

1 回答 1

25

我假设您使用的是 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 是对的。

于 2013-10-23T12:37:46.070 回答