gpt4 book ai didi

jersey - 使用 Jersey 客户端 2.22.1 在 GET 请求中关闭连接

转载 作者:行者123 更新时间:2023-12-04 05:11:54 35 4
gpt4 key购买 nike

我正在使用 Jersey 客户端从 Java 代码进行 REST 调用:

<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.22.1</version>
</dependency>

在我的 GET 请求中,

javax.ws.rs.client.Invocation.Builder builder = ClientBuilder.newClient().target(url).request(); 
builder.get().readEntity(String.class);

调用 readEntity(String.class)后客户端会自动关闭.

如果我使用,

builder.get(String.class);  

我得到相同的输出。

连接是自动关闭还是在这种情况下我需要手动关闭它?

最佳答案

简答

考虑以下代码:

Client client = ClientBuilder.newClient();
String result = client.target(url).request().get(String.class);

在幕后,Jersey 调用 Response#readEntity(Class<T>) 如果请求成功并且连接将为您关闭。所以在这种情况下不需要手动关闭连接。

现在考虑以下代码:

Client client = ClientBuilder.newClient();
Response response = client.target(url).request().get();

对于这种情况,您需要调用 Response#close() 关闭连接。或调用 Response#readEntity(Class<T>) 让 Jersey 为您关闭连接。

长答案

documentation 中所述, 如果你没有 read the entity ,那么您需要通过调用 Response#close() 手动关闭响应.

更多详情,请查看 Jersey 的 documentation关于如何关闭连接:

5.7. Closing connections

The underlying connections are opened for each request and closed after the response is received and entity is processed (entity is read). See the following example:

final WebTarget target = ... some web target
Response response = target.path("resource").request().get();
System.out.println("Connection is still open.");
System.out.println("string response: " + response.readEntity(String.class));
System.out.println("Now the connection is closed.");

If you don't read the entity, then you need to close the response manually by response.close().

Also if the entity is read into an InputStream (by response.readEntity(InputStream.class)), the connection stays open until you finish reading from the InputStream. In that case, the InputStream or the Response should be closed manually at the end of reading from InputStream.



此外,请查看 JerseyInvocation source .下面引用了最重要的部分。

translate(ClientResponse, RequestScope, Class<T>)方法你会看到 response.readEntity(Class<T>)被调用。

JerseyInvocation.Builder#get(Class<T>)

调用 HTTP GET同步当前请求的方法。

@Override
public <T> T get(final Class<T> responseType)
throws ProcessingException, WebApplicationException {

return method("GET", responseType);
}

JerseyInvocation.Builder#method(String, Class<T>)

同步调用当前请求的任意方法。

@Override
public <T> T method(final String name, final Class<T> responseType)
throws ProcessingException, WebApplicationException {

// responseType null check omitted for brevity

requestContext.setMethod(name);
return new JerseyInvocation(this).invoke(responseType);
}

JerseyInvocation#invoke(Class<T>)

同步调用请求并接收指定类型的响应。

@Override
public <T> T invoke(final Class<T> responseType)
throws ProcessingException, WebApplicationException {

// responseType null check omitted for brevity

final ClientRuntime runtime = request().getClientRuntime();
final RequestScope requestScope = runtime.getRequestScope();

return requestScope.runInScope(new Producer<T>() {

@Override
public T call() throws ProcessingException {

try {

return translate(runtime.invoke(requestForCall(requestContext)),
requestScope, responseType);

} catch (final ProcessingException ex) {
// Exception handling omitted for brevity
}
}
});
}
JerseyInvocation#translate(ClientResponse, RequestScope, Class<T>)
如果请求成功,则响应实体被读取为使用 Response#readEntity(Class<T>) 的指定 Java 类型的实例。 :

private <T> T translate(final ClientResponse response, final RequestScope scope, 
final Class<T> responseType) throws ProcessingException {

if (responseType == Response.class) {
return responseType.cast(new InboundJaxrsResponse(response, scope));
}

if (response.getStatusInfo().getFamily() == Response.Status.Family.SUCCESSFUL) {

try {

return response.readEntity(responseType);

} catch (final ProcessingException ex) {
// Exception handling omitted for brevity
}

} else {
throw convertToException(new InboundJaxrsResponse(response, scope));
}
}

关于jersey - 使用 Jersey 客户端 2.22.1 在 GET 请求中关闭连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35265534/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com