You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot 2.7.0无法注入RestTemplateBuilder问题求助

问题解决:Spring Boot 2.7.0中找不到RestTemplateBuilder Bean

问题重现

在Spring Boot 2.7.0版本中,定义如下RestTemplate Bean时出现错误:

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

相关代码:

@Bean
@Qualifier(REST_TEMPLATE_XXX)
public RestTemplate restTemplateteXXX(RestTemplateBuilder builder, Properties properties) {
   ...
}

该代码在Spring Boot 2.6.14版本中可正常运行。

解决方案

  • 检查自动配置是否被禁用
    确认启动类上没有通过@SpringBootApplication(exclude = ...)排除RestTemplateAutoConfiguration类。这个类负责自动注册RestTemplateBuilder Bean,2.7.0版本中它依然默认启用,若被排除会导致找不到Bean。

  • 确认依赖完整性
    确保项目中引入了spring-boot-starter-web依赖(Web项目场景),该依赖会间接引入包含RestTemplateAutoConfiguration的spring-boot-autoconfigure模块,保证自动配置逻辑生效。

  • 手动注册RestTemplateBuilder(兜底方案)
    如果自动配置因未知原因失效,可以手动在配置类中注册RestTemplateBuilder Bean:

    @Bean
    public RestTemplateBuilder restTemplateBuilder() {
        return new RestTemplateBuilder();
    }
    

内容的提问来源于stack exchange,提问作者Hesk

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 03:30:55