Reputation: 6254
I'm using Spring Boot 2.1.1, Spring Data REST, Spring HATEOAS, Hibernate.
In my @Configuration
class I created a custom RestTemplate
:
@Configuration
@EnableRetry
@EnableTransactionManagement
@EnableJpaAuditing(auditorAwareRef = "springSecurityAuditorAware")
public class CustomConfiguration {
@Bean
public RestTemplate restTemplate() {
RestTemplate restTemplate = new RestTemplateBuilder().setConnectTimeout(httpClientConnectionTimeout)
.setReadTimeout(httpClientReadTimeout).build();
ObjectMapper objectMapper = new ObjectMapper();
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter(objectMapper));
restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
restTemplate.getMessageConverters().add(0, new StringHttpMessageConverter(Charset.forName("UTF-8")));
return restTemplate;
}
I use this restTemplate in a @Component:
@Component
public class TenantRestClient {
@Autowired
private RestTemplate restTemplate;
public Tenant register(Tenant tenant) {
//my stuff here
}
Until Spring Boot 2.0.3 everything worked fine, but if I update to Spring 2.0.4 or greater, when I try to run my application I've this exception:
14/12/2018 09:09:55,442 INFO main testServerApplication:50 - Starting testServerApplication on SVILUPPO1 with PID 10612 (C:\Users\Daniele\Documents\workspaceREST2\test-server\target\classes started by Daniele in C:\Users\Daniele\Documents\workspaceREST2\test-management-server)
14/12/2018 09:09:55,448 DEBUG main testServerApplication:53 - Running with Spring Boot v2.1.1.RELEASE, Spring v5.1.3.RELEASE
14/12/2018 09:09:55,448 INFO main testServerApplication:679 - The following profiles are active: prod
14/12/2018 09:10:02,101 ERROR main TomcatStarter:62 - Error starting Tomcat context. Exception: org.springframework.beans.factory.UnsatisfiedDependencyException. Message: Error creating bean with name 'webSecurityConfiguration': Unsatisfied dependency expressed through method 'setContentNegotationStrategy' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$EnableWebMvcConfiguration': Unsatisfied dependency expressed through method 'setConfigurers' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'webMvcConfiguration': Unsatisfied dependency expressed through field 'tenantRestClient'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'tenantRestClient': Unsatisfied dependency expressed through field 'restTemplate'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'restTemplate' defined in class path resource [cloud/test/server/config/CustomConfiguration.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'org.springframework.hateoas.config.ConverterRegisteringWebMvcConfigurer' available: expected single matching bean but found 2: org.springframework.hateoas.config.ConverterRegisteringWebMvcConfigurer#0,org.springframework.hateoas.config.ConverterRegisteringWebMvcConfigurer#1
14/12/2018 09:10:02,124 WARN main WebappClassLoaderBase:173 - The web application [ROOT] appears to have started a thread named [lettuce-eventExecutorLoop-1-1] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
sun.misc.Unsafe.park(Native Method)
java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:215)
java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.awaitNanos(AbstractQueuedSynchronizer.java:2078)
java.util.concurrent.LinkedBlockingQueue.poll(LinkedBlockingQueue.java:467)
io.netty.util.concurrent.SingleThreadEventExecutor.takeTask(SingleThreadEventExecutor.java:251)
io.netty.util.concurrent.DefaultEventExecutor.run(DefaultEventExecutor.java:64)
io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:884)
io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
java.lang.Thread.run(Thread.java:748)
14/12/2018 09:10:02,128 WARN main AnnotationConfigServletWebServerApplicationContext:554 - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat
14/12/2018 09:10:02,163 ERROR main LoggingFailureAnalysisReporter:42 -
***************************
APPLICATION FAILED TO START
***************************
Description:
Field restTemplate in cloud.test.server.rest.clients.TenantRestClient required a single bean, but 2 were found:
- org.springframework.hateoas.config.ConverterRegisteringWebMvcConfigurer#0: defined in null
- org.springframework.hateoas.config.ConverterRegisteringWebMvcConfigurer#1: defined in null
Action:
Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed
Process finished with exit code 1
I tried to:
@Qualifier
on restTemplate
in my @Configuration
classrestTemplate
in my @Configuration
classThe first attempt did't change anything, with the latter I've this weir error:
***************************
APPLICATION FAILED TO START
***************************
Description:
Field restTemplate in cloud.test.server.rest.clients.TenantRestClient required a bean of type 'org.springframework.web.client.RestTemplate' that could not be found.
Action:
Consider defining a bean of type 'org.springframework.web.client.RestTemplate' in your configuration.
Do you have any hint to solve this strange error? And...why does it happen just updating to Spring Boot >=2.0.4 (same code)?
Upvotes: 2
Views: 5512
Reputation: 987
please try with below property in the application.properties of your spring boot project. Then it will allow you to override the existing bean definitions with our custom with the same name.
spring.main.allow-bean-definition-overriding=true
Upvotes: 1
Reputation: 21923
Try annotation restTemplate
bean with @Primary
.
@Bean
@Primary
public RestTemplate restTemplate() {
RestTemplate restTemplate = new RestTemplateBuilder().setConnectTimeout(httpClientConnectionTimeout)
.setReadTimeout(httpClientReadTimeout).build();
ObjectMapper objectMapper = new ObjectMapper();
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter(objectMapper));
restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
restTemplate.getMessageConverters().add(0, new StringHttpMessageConverter(Charset.forName("UTF-8")));
return restTemplate;
}
Upvotes: 1