Переопределить стандартный RestTemplate в Spring Boot 2.1.1 - PullRequest
0 голосов
/ 14 декабря 2018

Я использую Spring Boot 2.1.1, Spring Data REST, Spring HATEOAS, Hibernate.

В моем классе @Configuration я создал пользовательский 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;
    }

Я использую этот restTemplate в @Component:

@Component
public class TenantRestClient {

    @Autowired
    private RestTemplate restTemplate;

     public Tenant register(Tenant tenant) {
     //my stuff here
     }

До Spring Boot 2.0.3 все работало нормально, но если я обновляюсь до Spring 2.0.4 или выше, когда я пытаюсь запустить свое приложение, я 'Это исключение:

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

Я пытался:

  1. Установить @Qualifier на restTemplate в моем @Configuration классе
  2. Удалить компонентrestTemplate в моем @Configuration классе

Первая попытка ничего не изменила, с последней у меня есть такая странная ошибка:

***************************
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.

Есть ли у вас какие-либонамек решить эту странную ошибку?И ... почему происходит просто обновление до Spring Boot> = 2.0.4 (тот же код)?

1 Ответ

0 голосов
/ 14 декабря 2018

Попробуйте аннотацию restTemplate bean с @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;
}
...