<span>How to autowire RestTemplate using annotations</span>

背景说明:

今天,在复用其他项目的模块时候,引入maven后启动报错:

Ddsription:
Field restTemplate in xxx.impl.xxServiceImpl required a bean of type 'org.springframework.web.client.RestTemplate' that could not be found.
Action:
Consider defining a bean of type 'org.springframework.client.RestTemplate' in your configuration

问题很简单,就是你自动注入的bean没找到需要配置下呗。

原来的项目是配置在特定的地方,而模块里面并没有配置,引入模块,并未引入配置,因此需要配置bean:

@Configuration
public class CommonAutoConfiguration {
    @Bean
    public RestTemplate restTemplate(ClientHttpRequestFactory factory){
        return new RestTemplate(factory);
    }
    @Bean
    public ClientHttpRequestFactory simpleClientHttpRequestFactory(){
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        factory.setReadTimeout(5000);
        factory.setConnectTimeout(5000);
        return factory;
    }
} 

不同的设置参考 这里:How to autowire RestTemplate using annotations-stackOverflow

全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务