required a bean of type ‘org.springframework.web.client.RestTemplate‘ that could not be found.

简介: required a bean of type ‘org.springframework.web.client.RestTemplate‘ that could not be found.

问题


报错信息:


Could not autowire. No beans of 'RestTemplate' type found.

1.png


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

相关文章
|
4月前
|
Java Maven
Unable to start web server; nested exception is org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
Unable to start web server; nested exception is org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
76 0
|
XML Java 数据格式
【Spring】全面讲解IOC、AOP、注入方式、bean的生命周期、aop通知应用 spring与web容器整合
Spring是一个开源的轻量级Java应用开发框架,它提供了一种简单、高效、灵活的方式来构建企业级应用程序。Spring框架的核心特点是依赖注入(Dependency Injection)和面向切面编程(Aspect-Oriented Programming),它通过一组模块化的组件提供全面的支持,使开发人员能够快速搭建可扩展、可维护的应用。
|
7月前
SpringCloud启动Consider defining a bean of type ‘org.springframework.web.client.RestTemplate‘ in your
SpringCloud启动Consider defining a bean of type ‘org.springframework.web.client.RestTemplate‘ in your
135 1
|
前端开发 Java Spring
【小家Spring】Spring环境中(含Boot环境),web组件(Servlet、Filter)内注入使用Spring容器里的Bean
【小家Spring】Spring环境中(含Boot环境),web组件(Servlet、Filter)内注入使用Spring容器里的Bean
|
存储 前端开发 Java
【Java Web编程 五】深入理解Java Bean
【Java Web编程 五】深入理解Java Bean
167 0
|
Java Maven
Maven打包出现webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in update mode)
Maven打包出现webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in update mode)
749 0
There is no configured/running web-servers found Please, run any web-configuration and hit the
There is no configured/running web-servers found Please, run any web-configuration and hit the
139 0
|
应用服务中间件
TOMCAT部署项目报错,More than one fragment with the name [spring_web] was found.
TOMCAT部署项目报错,More than one fragment with the name [spring_web] was found.
342 0
TOMCAT部署项目报错,More than one fragment with the name [spring_web] was found.
|
前端开发 测试技术
单元测试Error creating bean with name org.springframework.web.servlet.resource.Resource
单元测试Error creating bean with name org.springframework.web.servlet.resource.Resource
319 0
单元测试Error creating bean with name org.springframework.web.servlet.resource.Resource
|
前端开发 Java 数据库连接
Java Web之Spring核心之IOC的解析和实战以及bean的使用
Java Web之Spring核心之IOC的解析和实战以及bean的使用
143 0
Java Web之Spring核心之IOC的解析和实战以及bean的使用