Proxy 代理设置在Jersey ClientConfig中不起作用

Proxy 代理设置在Jersey ClientConfig中不起作用,proxy,jersey,Proxy,Jersey,我正在尝试使用Jersey客户端在java代码中设置代理,但未设置代理。我阅读了Jersey文档,并以所描述的方式实现了代码。我是从新泽西到新泽西的,所以不知道我会错在哪里 下面是代码 @Override @CircuitBreaker(name = "documentServiceCreateDocument", ignore = { NullPointerException.class, ArrayIndexOutOfBoundsException.class }) publ

我正在尝试使用Jersey客户端在java代码中设置代理,但未设置代理。我阅读了Jersey文档,并以所描述的方式实现了代码。我是从新泽西到新泽西的,所以不知道我会错在哪里

下面是代码

@Override
@CircuitBreaker(name = "documentServiceCreateDocument", ignore = { NullPointerException.class,
        ArrayIndexOutOfBoundsException.class })
public String createDocument(String name, DocumentType docType, List<SourceData> sourceDatas) {
    ClientConfig clientConfig = new ClientConfig().register(MultiPartFeature.class)
            .register(ClientTransactionIdFilter.class)
            .property(ClientProperties.READ_TIMEOUT, "30000")
            .property(ClientProperties.CONNECT_TIMEOUT, "30000")
            .property(ClientProperties.PROXY_URI, properties.getProxyUrl);

    Client client = ClientBuilder.newClient(clientConfig);

    Builder builder = resourceTarget.request().header("Authorization", ***);
    List<Cookie> iamCookies = ***

    Response response = null;

    try {
        response = builder.post(body);

    } catch (Exception e){
        if(response != null) {
            logger.info("Response code : " + response.getStatus());
            logger.info("Response : " + response.toString());
        }
        e.printStackTrace();
        throw new RuntimeException(e);
    }
    String docLocation = response.getLocation().toString();
    logger.debug("Created Document Service document with location=" + docLocation);

    return docLocation;
}
@覆盖
@断路器(name=“documentServiceCreateDocument”,ignore={NullPointerException.class,
ArrayIndexOutOfBoundsException.class})
公共字符串createDocument(字符串名称、DocumentType docType、列表源数据){
ClientConfig ClientConfig=new ClientConfig().register(MultiPartFeature.class)
.register(ClientTransactionIdFilter.class)
.property(ClientProperties.READ_超时,“30000”)
.property(ClientProperties.CONNECT\u超时,“30000”)
.property(ClientProperties.PROXY_URI,properties.getProxyUrl);
Client Client=ClientBuilder.newClient(clientConfig);
Builder=resourceTarget.request().header(“授权”,***);
列表cookies=***
响应=空;
试一试{
响应=builder.post(body);
}捕获(例外e){
if(响应!=null){
logger.info(“响应代码:+Response.getStatus());
logger.info(“响应:+Response.toString());
}
e、 printStackTrace();
抛出新的运行时异常(e);
}
字符串docLocation=response.getLocation().toString();
logger.debug(“使用location=“+docLocation”)创建文档服务文档);
返回文档位置;
}

经过长时间的努力,我终于找到了解决办法。我们需要使用ApacheConnectorProvider才能使代理正常工作

将ApacheConnectorProvider添加到ClientConfig,如下所示:

ClientConfig clientConfig = new ClientConfig().register(MultiPartFeature.class)
        .register(ClientTransactionIdFilter.class)
        .property(ClientProperties.READ_TIMEOUT, "30000")
        .property(ClientProperties.CONNECT_TIMEOUT, "30000")
        .connectorProvider(new ApacheConnectorProvider())
        .property(ClientProperties.PROXY_URI, properties.getProxyUrl);
不要忘记将jersey apache连接器依赖项添加到pom文件中(如果您使用的是maven)。有关jersey apache连接器依赖项的详细信息,请参阅以下链接: