Spring Boot Rest模板 - CloseableHttpClient

时间:2017-02-27 16:04:39

标签: spring spring-boot spring-rest

我在Spring启动应用程序中定义了一个类型为CloseableHttpClient的bean。但是我仍然得到了无法找到bean的错误。

@Bean
@Primary
public RestTemplate restTemplate(RestTemplateBuilder builder, @Qualifier("pooledClient") CloseableHttpClient httpClient) {
    return builder.requestFactory(new HttpComponentsClientHttpRequestFactory(httpClient)).build();
}

@Bean
public CloseableHttpClient httpClient() {
    return HttpClientBuilder.create().build();
}

com.MyConfiguration中方法restTemplate的参数1需要一个类型为' org.apache.http.impl.client.CloseableHttpClient'的bean。无法找到。

动作:

考虑定义一个类型为#org.apache.http.impl.client.CloseableHttpClient'的bean。在你的配置中。

1 个答案:

答案 0 :(得分:2)

您正在使用@Qualifier作为CloseableHttpClient,但在您的配置中,您尚未定义任何与该限定符匹配的bean。要么声明一个名为pooledClient的bean:

@Bean(name="pooledClient")
public CloseableHttpClient httpClient() {
    return HttpClientBuilder.create().build();
}

或者删除@Qualifier注释:

@Bean
@Primary
public RestTemplate restTemplate(RestTemplateBuilder builder, CloseableHttpClient httpClient) {
    return builder.requestFactory(new HttpComponentsClientHttpRequestFactory(httpClient)).build();
}