ApacheConnectorProvider:泽西客户端 2.5.1

2023-12-24

Ref: https://jersey.java.net/documentation/latest/user-guide.html#d0e4337 https://jersey.java.net/documentation/latest/user-guide.html#d0e4337。 我正在尝试使用ApacheConnector作为球衣客户端的连接器。客户端似乎在 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作为连接器。如何使用为客户端配置的连接器?


将连接器设置为ClientConfig不是相反(ConnectorProvider#getConnector不应该由用户调用,而是由 Jersey Client 调用,它是 SPI 的一部分):

ClientConfig clientConfig = new ClientConfig();
clientConfig.connectorProvider(new ApacheConnectorProvider());
Client client = ClientBuilder.newClient(clientConfig);

泽西岛用户指南中对此进行了描述 -客户端传输连接器 https://jersey.java.net/documentation/latest/client.html#d0e4338.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

ApacheConnectorProvider:泽西客户端 2.5.1 的相关文章

随机推荐