问题
报错信息:
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.的全部内容,如果对你有帮助感谢点赞支持!