0
votes

I am defining a bean of type CloseableHttpClient in my Spring boot app. But still I get the error that the bean could not be found.

@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();
}

Parameter 1 of method restTemplate in com.MyConfiguration required a bean of type 'org.apache.http.impl.client.CloseableHttpClient' that could not be found.

Action:

Consider defining a bean of type 'org.apache.http.impl.client.CloseableHttpClient' in your configuration.

1

1 Answers

3
votes

You are using a @Qualifier for the CloseableHttpClient but in your config you haven't defined any bean that matches that Qualifier. Either you declare a bean named pooledClient:

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

Or you remove the @Qualifier annotation:

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