Jersey客户端抛出"无法使用不可重复的请求实体重试请求。"在代理服务器上进行POST时

时间:2017-02-09 14:35:17

标签: java jersey jersey-2.0 jersey-client

在下文中,我使用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;
    }
}

1 个答案:

答案 0 :(得分:1)

看起来像个错误。实际上,这个布尔配置参数会发生什么,它会为BufferedHttpEntity创建一个新的字节数组,这是处理请求所必需的。

您可以在ApacheConnector中的jersey-apache-connector-2.22.4.jar的类method getUriHttpRequest(...)中找到该代码。