在下文中,我使用Jersey 2.25使用ApacheConnector通过代理执行POST请求。由于代理,如果我省略配置:
config.property(ClientProperties.REQUEST_ENTITY_PROCESSING, RequestEntityProcessing.BUFFERED);
我会收到错误Caused by: org.apache.http.client.NonRepeatableRequestException: Cannot retry request with a non-repeatable request entity.
问题:使用Jersey 2处理代理配置的正确方法是什么?我的解决方案对我来说就像是一块垃圾。
package JerseyTest.one;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.Invocation.Builder;
import javax.ws.rs.core.MediaType;
import org.glassfish.jersey.apache.connector.ApacheConnectorProvider;
import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.client.ClientProperties;
import org.glassfish.jersey.client.RequestEntityProcessing;
public class Requester {
public static String getPostResponseInOne(final String url, final String json) {
final ClientConfig config = new ClientConfig();
config.connectorProvider(new ApacheConnectorProvider());
config.property(ClientProperties.PROXY_URI, "http://proxyurl:8080");
config.property(ClientProperties.PROXY_USERNAME,"michael");
config.property(ClientProperties.PROXY_PASSWORD,"secret");
config.property(ClientProperties.REQUEST_ENTITY_PROCESSING, RequestEntityProcessing.BUFFERED);
final Client client = ClientBuilder.newClient(config);
final Builder builder = client.target(url).request(MediaType.APPLICATION_JSON);
final String response = builder.post(Entity.json(json), String.class);
return response;
}
}
答案 0 :(得分:1)
看起来像个错误。实际上,这个布尔配置参数会发生什么,它会为BufferedHttpEntity
创建一个新的字节数组,这是处理请求所必需的。
您可以在ApacheConnector
中的jersey-apache-connector-2.22.4.jar的类method getUriHttpRequest(...)
中找到该代码。