Issue
I'm working on a Spring Boot project that includes 2 maven dependencies, those dependencies both use org.springframework.web.client.RestTemplate
so when in my project I do:
@Autowired
AClassUsingRestTemplate object;
it triggers:
No qualifying bean of type 'org.springframework.web.client.RestTemplate' available: expected single matching bean but found 2: externalDependencyOneRestTemplate,externalDependencyTwoRestTemplate
I have the control of only one of those dependencies, where I've defined:
@Bean(name = "externalDependencyOneRestTemplate")
public RestTemplate buildExternalDependencyOneRestTemplate() {
final HttpComponentsClientHttpRequestFactory clientHttpRequestFactory =
new HttpComponentsClientHttpRequestFactory(HttpClientBuilder.create().build());
final RestTemplate restTemplate = new RestTemplate(clientHttpRequestFactory);
restTemplate.setRequestFactory(new BufferingClientHttpRequestFactory(restTemplate.getRequestFactory()));
return restTemplate;
}
what can I do to fix it?
Do I have to tell ClassUsingRestTemplate
to use a specifi RestTemplate
implementations? If yes, how to do that?
Thanks
Solution
Specify which one you want using @Qualifier
:
@Autowired
@Qualifier("externalDependencyOneRestTemplate")
RestTemplate restTemplate;
If you cannot modify the code depending on the RestTemplate
, you can specify which one is the default, i.e. which one to use if @Qualifier
is not specified:
@Primary
@Bean(name = "externalDependencyOneRestTemplate")
public RestTemplate buildExternalDependencyOneRestTemplate() {
...
}
Answered By - Andreas