加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

How to autowire RestTemplate using annotations

发布时间:2020-12-15 08:24:39 所属栏目:Java 来源:网络整理
导读:背景说明: 今天,在复用其他项目的模块时候,引入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

背景说明:

今天,在复用其他项目的模块时候,引入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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读