问题
Going from com.sun.jersey.api.client.Client to javax.ws.rs.client.Client how do I configure 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.
I am using javax.ws.rs-api-2.0.jar
回答1:
I assume you are using jax-rs-ri. For this, you can use ClientProperties.CONNECT_TIMEOUT and ClientProperties.READ_TIMEOUT.
Example:
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);
EDIT:
I read the API document for ClientConfig.property. And @Gili is right.
来源:https://stackoverflow.com/questions/19539970/javax-ws-rs-client-client-how-to-configure-readtimeout