我尝试使用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;
}
答案 0 :(得分: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);
不要忘记将jersey-apache-connector依赖项添加到您的pom文件中(如果您使用的是maven)。有关jersey-apache-connector依赖项详细信息,请参阅以下链接: https://mvnrepository.com/artifact/org.glassfish.jersey.connectors/jersey-apache-connector/2.6