参考:https://jersey.java.net/documentation/latest/user-guide.html#d0e4337。
我正在尝试使用ApacheConnector
作为jersey-client的连接器。客户端似乎在2.4.1版本的jersey-client和apache连接器中运行良好。
上述网站上的使用文档有一个注释:
此API在Jersey 2.5中已更改,其中引入了
ConnectorProvider
SPI,以便将客户端初始化与连接器实例化分离。从Jersey 2.5开始,因此无法在Jersey ClientConfig中直接注册Connector实例。必须使用新的ConnectorProvider SPI来配置自定义客户端传输连接器。
public Client configureDefaultJerseyClient(String host) throws Exception
{
String certFilePath = InstallCert.doInstall(host,SSL_PORT,TRUST_STORE_PASSWORD);
if(EMPTY_STRING.equals(certFilePath))
{
throw new Exception("Error while installing certificate for host " + host);
}
ClientConfig clientConfig = new ClientConfig();
/* As the PoolingClientConnectionManager is a deprecated class, the client will
not support the multithreaded requests. Commenting the code below to avoid using
deprecated class. In order to test we would be instantiating multiple clients to
serve the multithreaded requests.*/
clientConfig.property(ApacheClientProperties.CONNECTION_MANAGER, new PoolingHttpClientConnectionManager());
SslConfigurator sslConfig = defaultSslConfigurator(certFilePath);
clientConfig.property(ApacheClientProperties.SSL_CONFIG, sslConfig);
SSLContext sslContext = sslConfig.createSSLContext();
clientConfig.property(ApacheClientProperties.SSL_CONFIG, sslConfig);
Client client = ClientBuilder.newBuilder().sslContext(sslContext).build();
client.register(new MyFilter());
client.register(new org.glassfish.jersey.filter.LoggingFilter());
ApacheConnectorProvider provider = new ApacheConnectorProvider();
provider.getConnector(client, clientConfig);
return client;
}
但似乎客户端始终使用默认的HttpUrlConnection
作为连接器。如何使用为客户端配置的连接器?
答案 0 :(得分:4)
将连接器设置为ClientConfig
而不是其他方式(ConnectorProvider#getConnector
不应由用户调用,而是由Jersey客户端调用,它是SPI的一部分:
ClientConfig clientConfig = new ClientConfig();
clientConfig.connectorProvider(new ApacheConnectorProvider());
Client client = ClientBuilder.newClient(clientConfig);
泽西岛用户指南 - Client Transport Connectors中对此进行了描述。