descripción incorrecta
Description:
Field restTemplate in com.hadoopx.quartz.executor.ServicexExecutor required a bean of type 'org.springframework.web.client.RestTemplate' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'org.springframework.web.client.RestTemplate' in your configuration.
análisis de errores
Porque en SpringBoot-1.3 y versiones anteriores, se proporciona un BEAN de instancia RestTemplate de forma predeterminada, pero en SpringBoot-1.4 y versiones posteriores, este BEAN predeterminado ya no se proporciona.
Solución①:
// 创建一个配置类, 在配置类中手动注入RestTemplate对象。
@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate(ClientHttpRequestFactory factory) {
return new RestTemplate(factory);
}
@Bean
public ClientHttpRequestFactory simpleClientHttpRequestFactory() {
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
// 连接超时
factory.setConnectTimeout(15000);
// 数据读取超时时间
factory.setReadTimeout(5000);
return factory;
}
}
Solución ②:
// 在 SpringBoot 项目启动类的Application 中,手动注入RestTemplate对象。
@Bean
// Spring Cloud的注解, 有该注解则RestTemplate就开启负载均衡能力, 也可以没有该注解。
@LoadBalanced
RestTemplate restTemplate(){
return new RestTemplate();
}
extensión del conocimiento
- La anotación @LoadBalanced se encuentra en el paquete org.springframework.cloud.client.loadbalancer;
- Cuando se inyecta manualmente RestTemplate, esta anotación generalmente se agrega, lo que indica que RestTemplate con esta anotación habilitará el equilibrio de carga;