required a bean of type ‘org.springframework.web.client.RestTemplate‘ that could not be found.
Posted 牛哄哄的柯南
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了required a bean of type ‘org.springframework.web.client.RestTemplate‘ that could not be found.相关的知识,希望对你有一定的参考价值。
问题
报错信息:
Could not autowire. No beans of 'RestTemplate' type found.
Description:
Field restTemplate in com.tky.bim.basedata.service.impl.RelationV1Update 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.
分析问题:
错误提示说RestTemplate没找到,这是因为在 Spring Boot 1.3版本中,会默认提供一个RestTemplate的实例Bean,而在 Spring Boot 1.4以及以后的版本中,这个默认的bean不再提供了,我们需要在Application启动时,手动创建一个RestTemplate的配置。
解决方案
方案一
创建个配置类,在配置类中手动注入RestTemplate。
ConfigBean.java
package com.keafmd.springcloud.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
/**
* Keafmd
*
* @ClassName: Configbean
* @Description:
* @author: 牛哄哄的柯南
* @date: 2021-07-21 15:40
*/
@Configuration
public class ConfigBean { //@Configuration 相当于 spring中的 application.xml
@Bean
RestTemplate restTemplate(){
return new RestTemplate();
}
}
方案二
直接在启动类中注入RestTemplate也可以。
DeptConsumer_80 .java
package com.keafmd.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
/**
* Keafmd
*
* @ClassName: DeptConsumer_80
* @Description: 启动类
* @author: 牛哄哄的柯南
* @date: 2021-07-21 16:15
*/
@SpringBootApplication
public class DeptConsumer_80 {
public static void main(String[] args) {
SpringApplication.run(DeptConsumer_80.class,args);
}
@Bean
RestTemplate restTemplate(){
return new RestTemplate();
}
}
以上就是required a bean of type ‘org.springframework.web.client.RestTemplate’ that could not be found.的全部内容,如果对你有帮助感谢点赞支持!
版权声明:
原创博主:牛哄哄的柯南
博主原文链接:https://keafmd.blog.csdn.net/
看完如果对你有帮助,感谢点赞支持!
如果你是电脑端,看到右下角的 “一键三连” 了吗,没错点它[哈哈]
加油!
共同努力!
Keafmd
以上是关于required a bean of type ‘org.springframework.web.client.RestTemplate‘ that could not be found.的主要内容,如果未能解决你的问题,请参考以下文章
Not registered via @EnableConfigurationProperties, | A component required a bean of type ‘xxx‘
Not registered via @EnableConfigurationProperties, | A component required a bean of type ‘xxx‘
Not registered via @EnableConfigurationProperties, | A component required a bean of type ‘xxx‘
A compent required a bean of type ‘java.lang.String‘ that could not found
A compent required a bean of type ‘java.lang.String‘ that could not found
A component required a bean of type ‘com.wyh.service.XXX‘ that could not be found.