springcloud 项目搭建遇到的问题及解决-Field restTemplate in com.cloud.ribbon_consumer.project.service.xxxService required a bean of type 'org.springframework.web.client.RestTemplate' that could not be found.


原文链接:https://blog.csdn.net/weixin_44259720/article/details/109216423

问题描述:
搭建SpringCloud 架构,配置者服务,启动该服务时候报出标题中的错误,导致程序启动失败。

完整的错误信息如下:

Field restTemplate in com.cloud.ribbon_consumer.project.service.xxxService required a bean of type 'org.springframework.web.client.RestTemplate' that could not be found.

其中,“com.cloud.ribbon_consumer.project.service.xxxService”是我的示例中使用RestTemplate实例的路径。

spring cloud 架构开发中,有两种消费的方式:RestTemplate+ribbon,feign。

两者最大的区别就是区别就是:

feign 消费是通过注解的方式进行的消费模式,它默认打开了负载均衡;
而 ribbon 消费需要手动配置负载均衡;
其中,使用“RestTemplate + ribbon”方式配置消费时,当我们将 RestTemplate 通过 @Autowired 注解注入到一个类中,启动服务就可能会报该(标题中的)错误。

【原因在于】:

在 Spring Boot 1.3版本中,会默认提供一个RestTemplate的实例Bean,而在 Spring Boot 1.4以及以后的版本中,这个默认的bean不再提供了,我们需要在Application启动时,手动创建一个RestTemplate的配置。

这样,我们在类中通过注解 @Autowired 使用 TestTemplate 的时候,程序就可以找到被实例化的 TestTemplate,就不会出现上述的报错了。

解决方案:
在 Spring Boot 项目启动类 xxxApplication 中,设置手动引入 RestTemplate 相关配置,代码如下:

@SpringBootApplication(scanBasePackages = "com.xxx")
@EnableDiscoveryClient
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class);
}

@Bean
@LoadBalanced
RestTemplate restTemplate(){
    return new RestTemplate();
}

}

ps:原第三方RestTemplate调用换成httpclient调用


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



猜您在找 Field client in com.rachel.web.ConsumerController required a bean of type 'org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient' that could not be found. Field redisTemplate in xxxxxx required a bean of type 'org.springframework.data.redis.core.RedisTemplate' that could not be found. Field tTypeMapper in com.atguigu.project.service.imp.projectInfoServiceImpl required a bean of type 'com.atguigu.project.mapper.TTypeMapper' that could not be found. 2. springboot启动报错:Field userMapper in com.service.UserService required a bean of type 'com.dao.UserMapper' that could not be found. springcloud项目启动gateway报错org.springframework.cloud.gateway.config.GatewayAutoConfiguration required a bean of type 'org.springframework.http.codec.ServerCodecConfigurer' that could not be found Spring Cloud Ribbon负载均衡配置类放在Spring boot主类同级增加Exclude过滤后报Field config in com.cloud.web.controller.RibbonConfiguration required a bean of type 'com.netflix.client.config.IClientConfig' that could not b 运行springboot项目报错:Field userMapper in XX required a bean of type 'xx' that could not be found. Description: Field ud in com.yjj.service.impl.UserServiceImpl required a bean of type 'com.yjj.dao.UserDao' that could not be found. Action: Consider defining a bean of type 'com.yjj.dao.UserDao' Field userService in com.wuji.controller.UserController required a bean of type 'com.wuji.service.UserService' that could not be found Field adminMapper in com.course.service.impl.AdminServiceImpl required a bean of type 'com.course.mapper.AdminMapper' that could not be found
 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM