简体   繁体   English

我怎样才能在下面使用带有Apache httpclient的jersey-client?

[英]How can I use jersey-client with Apache httpclient underneath?

I am using jersey-client for a project and would like to make the Client use an HTTP client from the Apache httpclient librabry. 我正在为项目使用jersey-client ,并希望Client使用Apache httpclient librabry中的HTTP客户端。

I have previously see this is possible. 我以前看到这是可能的。

I'm using Jersey 2.20. 我正在使用Jersey 2.20。

Use ApacheConnectorProvider . 使用ApacheConnectorProvider Pass an instance to ClientConfig.connectorProvider() to get an instance of ClientConfig that will use an Apache HTTP client under the hood. 将实例传递给ClientConfig.connectorProvider()以获取ClientConfig的实例,该实例将使用Apache HTTP客户端。

Use the following dependency: 使用以下依赖项:

<dependency>
        <groupId>org.glassfish.jersey.connectors</groupId>
        <artifactId>jersey-apache-connector</artifactId>
        <version>2.20</version>
</dependency>

Here's a working example: 这是一个有效的例子:

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;

import org.glassfish.jersey.apache.connector.ApacheConnectorProvider;
import org.glassfish.jersey.client.ClientConfig;


public class Test {
    @org.junit.Test
    public void test() {
        ClientConfig cc = new ClientConfig().connectorProvider(new ApacheConnectorProvider());
        Client client = ClientBuilder.newClient(cc);
        System.out.println(client.target("http://example.com/").request().get().getStatus());
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM