从 com.sun.jersey.api.client.Client 转到 javax.ws.rs.client.Client 如何配置客户端?
FROM:
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.
TO:
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.
答案 0 :(得分:20)
我假设您使用的是jax-rs-ri。为此,您可以使用ClientProperties.CONNECT_TIMEOUT
和ClientProperties.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是对的。