ApacheConnectorProvider:Jersey客户端2.5.1

 4号的国哥 发布于 2023-01-29 12:25

参考:https : //jersey.java.net/documentation/latest/user-guide.html#d0e4337。我正在尝试将ApacheConnector用作泽西客户端的连接器。该客户端在jersey-client和apache连接器的2.4.1版本中似乎工作正常。

提到的网站上的用法文档中有一条注释:

此API在Jersey 2.5中进行了更改,其中ConnectorProvider引入了SPI,以使客户端初始化与连接器实例脱钩。从Jersey 2.5开始,因此不可能直接在Jersey ClientConfig中注册连接器实例。必须改用新的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作为连接器。如何使用为客户端配置的连接器?

1 个回答
  • 将连接器设置为ClientConfig其他方式(ConnectorProvider#getConnector不应由用户调用,而是由Jersey Client调用,它是SPI的一部分):

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

    泽西岛用户指南- 客户端传输连接器中对此进行了描述。

    2023-01-29 12:27 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有