如何使用 Jersey 2.x 设置连接和读取超时?
在球衣 1 中,我们有一个函数 com.sun.jersey.api.client.Client 类中的 .lang.Integer%29" rel="noreferrer">setConnectTimeout.
In jersey 1 we had a function setConnectTimeout in the class com.sun.jersey.api.client.Client
.
在 jersey 2 中,javax.ws.rs.client.Client
类用于缺少此功能的地方.
In jersey 2 the javax.ws.rs.client.Client
class is used where this function is missing.
jersey 2.x中如何设置连接超时和读取超时?
How to set connection timeout and read timeout in jersey 2.x?
推荐答案
下面的代码在 Jersey 2.3.1 中适用于我(灵感在这里找到:https://stackoverflow.com/a/19541931/1617124)
The code below works for me in Jersey 2.3.1 (inspiration found here: https://stackoverflow.com/a/19541931/1617124)
public static void main(String[] args) {
Client client = ClientBuilder.newClient();
client.property(ClientProperties.CONNECT_TIMEOUT, 1000);
client.property(ClientProperties.READ_TIMEOUT, 1000);
WebTarget target = client.target("http://1.2.3.4:8080");
try {
String responseMsg = target.path("application.wadl").request().get(String.class);
System.out.println("responseMsg: " + responseMsg);
} catch (ProcessingException pe) {
pe.printStackTrace();
}
}
相关文章