代理设置在Jersey ClientConfig中不起作用 [英] Proxy setting not working in Jersey ClientConfig

查看:230
本文介绍了代理设置在Jersey ClientConfig中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Jersey客户端在Java代码中设置代理,但未设置代理.我浏览了Jersey文档,并以所述方式实现了代码.我是新泽西州的新手,所以不确定我要去哪里.

I'm trying to setup a proxy in my java code using Jersey client but the proxy is not getting set. I went through the Jersey documentation and have implemented the code in the described way. I'm new to Jersey so not sure where I'm going wrong.

下面是代码.

@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;
}

推荐答案

经过很长一段时间,我终于找到了解决方法.我们需要使用ApacheConnectorProvider才能使代理正常工作.

After a long duration, I finally figured out the fix. We need to use the ApacheConnectorProvider in order for the proxy to work.

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

Add the ApacheConnectorProvider to the ClientConfig as shown below:

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-connector依赖项添加到pom文件中(如果使用的是maven).请参阅以下链接,获取jersey-apache-connector依赖项的详细信息: https://mvnrepository.com/artifact/org. glassfish.jersey.connectors/jersey-apache-connector/2.6

Don't forget to add the jersey-apache-connector dependency to your pom file(if you are using maven). Refer to the below link for jersey-apache-connector dependency details: https://mvnrepository.com/artifact/org.glassfish.jersey.connectors/jersey-apache-connector/2.6

这篇关于代理设置在Jersey ClientConfig中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆