ApacheConnectorProvider : Jersey Client 2.5.1

拥有回忆 提交于 2019-12-08 01:39:17

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/21570476/apacheconnectorprovider-jersey-client-2-5-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!