代理设置不泽西ClientConfig工作(Proxy setting not working in

2019-11-04 20:05发布

我试图安装一个代理在我的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;
}

Answer 1:

持续时间长后,我终于想通了修复。 我们需要使用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);

不要忘了球衣的Apache连接器的依赖添加到您的POM文件(如果你使用maven)。 请参阅下面的链接,球衣的Apache连接器的依赖细节: https://mvnrepository.com/artifact/org.glassfish.jersey.connectors/jersey-apache-connector/2.6



文章来源: Proxy setting not working in Jersey ClientConfig
标签: proxy jersey