Proxy setting not working in Jersey ClientConfig

怎甘沉沦 提交于 2019-12-25 01:45:01

问题


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.

Below is the code.

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

回答1:


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

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);

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



来源:https://stackoverflow.com/questions/50699520/proxy-setting-not-working-in-jersey-clientconfig

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!