ApacheConnectorProvider:Jersey客户端2.5.1 [英] ApacheConnectorProvider : Jersey Client 2.5.1

查看:491
本文介绍了ApacheConnectorProvider:Jersey客户端2.5.1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

参考: https://jersey.java.net/documentation/最新/用户guide.html#d0e4337
我试图使用 ApacheConnector 作为球衣的客户端的连接器。客户似乎在2.4.1版球衣的客户端和Apache连接器,做工精细。

中提到的网站上的使用文档有一个注意:


  

这个API已经在泽西岛2.5,其中 ConnectorProvider SPI是为了从连接器实例脱钩初始化客户端引入了改变。与新泽西州2.5开始因此它不可能直接注册Connector实例在泽西ClientConfig。新ConnectorProvider SPI必须改用配置自定义客户端的传输接口。


 公共客户configureDefaultJerseyClient(String host)在抛出异常
{
    字符串certFilePath = InstallCert.doInstall(主机,SSL_PORT,TRUST_STORE_PASSWORD);
    如果(EMPTY_STRING.equals(certFilePath))
    {
        抛出新的异常(错误在安装的主机证书+主机);
    }
    ClientConfig clientConfig =新ClientConfig();    / *由于PoolingClientConnectionManager是去precated类,客户端会
    不支持多线程请求。在谈到下code,以避免使用
    德precated类。为了测试我们将实例多个客户端
    服务于多线程的请求。* /    clientConfig.property(ApacheClientProperties.CONNECTION_MANAGER,新PoolingHttpClientConnectionManager());    SslConfigurator sslConfig = defaultSslConfigurator(certFilePath);
    clientConfig.property(ApacheClientProperties.SSL_CONFIG,sslConfig);    的SSLContext的SSLContext = sslConfig.createSSLContext();
    clientConfig.property(ApacheClientProperties.SSL_CONFIG,sslConfig);    Client客户端= ClientBuilder.newBuilder()的SSLContext(的SSLContext).build();
    client.register(新MyFilter());
    client.register(新org.glassfish.jersey.filter.LoggingFilter());    ApacheConnectorProvider提供商=新ApacheConnectorProvider();
    provider.getConnector(客户端,clientConfig);    返回客户端;
}

不过好像客户端始终使用默认 HttpURLConnection类为连接器。如何使用配置客户端的连接?


解决方案

连接设置为 ClientConfig 不是其他各地的方式( ConnectorProvider#getConnector 不应该由用户,但通过Jersey客户端调用,这是SPI的一部分):

  ClientConfig clientConfig =新ClientConfig();
clientConfig.connectorProvider(新ApacheConnectorProvider());
Client客户端= ClientBuilder.newClient(clientConfig);

这是泽西用户手册中描述的 - 客户端传输连接器

Ref: https://jersey.java.net/documentation/latest/user-guide.html#d0e4337. I am trying to use the ApacheConnector as a Connector for the jersey-client. The client seemed to work fine in the 2.4.1 version of jersey-client and apache connector.

The usage docs on the site mentioned has a note:

This API has changed in Jersey 2.5, where the ConnectorProvider SPI has been introduced in order to decouple client initialization from the connector instantiation. Starting with Jersey 2.5 it is therefore not possible to directly register Connector instances in the Jersey ClientConfig. The new ConnectorProvider SPI must be used instead to configure a custom client-side transport connector.

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

But it seems the client always uses the default HttpUrlConnection as a connector. How do I use the connector configured for the client?

解决方案

Set the connector to the ClientConfig not other way around (ConnectorProvider#getConnector isn't supposed to be called by users but by Jersey Client, it's part of SPI):

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

This is described in Jersey User Guide - Client Transport Connectors.

这篇关于ApacheConnectorProvider:Jersey客户端2.5.1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆