错误如下:

ERROR 31473 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
*************************** Description: Field restTemplate in org.springframework.cloud.zookeeper.discovery.dependency.DependencyRestTemplateAutoConfiguration required a bean of type 'org.springframework.web.client.RestTemplate' that could not be found. Action: Consider defining a bean of type 'org.springframework.web.client.RestTemplate' in your configuration.

解决方法:

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate; @Configuration
public class Config { @Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new
RestTemplate();
}

}

说明:可以封装一个Cinfig类,最主要是红色部分的RestTemplate,当然,可以直接在别的地方注入红色部分代码即可。而且,如果哪个组件上注解了这个方法,其余都可以不用,只是一次注解即可。

解释说明:

以上的代码是从官方的例子代码发现的:https://github.com/spring-cloud/spring-cloud-zookeeper/blob/master/spring-cloud-zookeeper-sample/src/main/java/org/springframework/cloud/zookeeper/sample/SampleZookeeperApplication.java

如果RestTemplate没有定义,您将看到错误

Consider defining a bean of type 'org.springframework.web.client.RestTemplate' in your configuration.

或者

No qualifying bean of type [org.springframework.web.client.RestTemplate] found

如何通过注解定义RestTemplate

这取决于你使用的是什么版本的技术会影响你如何定义你的“配置类RestTemplate。

Spring >=4且没有Spring Boot

简单地定义一个@Bean

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

Spring Boot<=1.3

无需定义,Spring Boot自动为您定义了一个。

Spring Boot >= 1.4

Spring Boot不再自动定义一个RestTemplate,而是定义了一个RestTemplateBuilder允许您更好地控制所RestTemplate创建的对象。你可以RestTemplateBuilder在你的@Bean方法中注入一个参数来创建一个RestTemplate

@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
// Do any additional configuration here
return builder.build();
}

在你的类上使用它

@Autowired
private RestTemplate restTemplate;

或者

@Inject
private RestTemplate restTemplate;

参考:

https://stackoverflow.com/questions/28024942/how-to-autowire-resttemplate-using-annotations

https://gist.github.com/RealDeanZhao/38821bc1efeb7e2a9bcd554cc06cdf96

Spring Cloud ZooKeeper集成Feign的坑1,错误:Consider defining a bean of type 'org.springframework.web.client.RestTemplate' in your configuration.的更多相关文章

  1. Spring Cloud ZooKeeper集成Feign的坑2,服务调用了一次后第二次调用就变成了500,错误:Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is com.n

    错误如下: 2017-09-19 15:05:24.659 INFO 9986 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refre ...

  2. Spring Cloud ZooKeeper集成Feign的坑3,程序Run模式运行没事,Debug模式下报错

    请更新Spring Cloud的版本: <dependency> <groupId>org.springframework.cloud</groupId> < ...

  3. spring cloud gateway网关启动报错:No qualifying bean of type 'org.springframework.web.reactive.DispatcherHandler'

    网关配置好后启动报错如下: org.springframework.context.ApplicationContextException: Unable to start web server; n ...

  4. 【spring boot】mybatis启动报错:Consider defining a bean of type 'com.newhope.interview.dao.UserMapper' in your configuration. 【Mapper类不能被找到】@Mapper 和@MapperScan注解的区别

    启动报错: 2018-05-16 17:22:58.161 ERROR 4080 --- Disconnected from the target VM, address: '127.0.0.1:50 ...

  5. SpringBoot- springboot集成Redis出现报错:No qualifying bean of type 'org.springframework.data.redis.connection.RedisConnectionFactory'

    Springboot将accessToke写入Redisk 缓存,springboot集成Redis出现报错 No qualifying bean of type 'org.springframewo ...

  6. Spring Cloud(Dalston.SR5)--Feign 声明式REST客户端

    Spring Cloud 对 Feign 进行了封装,集成了 Ribbon 并结合 Eureka 可以实现客户端的负载均衡,Spring Cloud 实现的 Feign 客户端类名为 LoadBala ...

  7. Spring Cloud 入门 之 Feign 篇(三)

    原文地址:Spring Cloud 入门 之 Feign 篇(三) 博客地址:http://www.extlight.com 一.前言 在上一篇文章<Spring Cloud 入门 之 Ribb ...

  8. 0.9.0.RELEASE版本的spring cloud alibaba sentinel+feign降级处理实例

    既然用到了feign,那么主要是针对服务消费方的降级处理.我们基于0.9.0.RELEASE版本的spring cloud alibaba nacos+feign实例添油加醋,把sentinel功能加 ...

  9. 解决spring-test中Feign问题: No qualifying bean of type 'org.springframework.cloud.openfeign.FeignContext' available

    问题现象: 启动测试类(含通过Feign远程调用的组件),报错: No qualifying bean of type 'org.springframework.cloud.openfeign.Fei ...

随机推荐

  1. scala学习笔记1: scala method

    刚接触scala,做练习的时候碰到一个问题,顺便mark一下. 先看下面一段代码: def sum(args:Int*) = { var result = 0 for (arg <- args) ...

  2. 分布式爬虫系统设计、实现与实战:爬取京东、苏宁易购全网手机商品数据+MySQL、HBase存储

    http://blog.51cto.com/xpleaf/2093952 1 概述 在不用爬虫框架的情况,经过多方学习,尝试实现了一个分布式爬虫系统,并且可以将数据保存到不同地方,类似MySQL.HB ...

  3. SuperSocket学习笔记(一)-一个完整的例子

    一.什么是SuperSocket 以下是作者的介绍 执行以下命令,获取SuperSocket项目 $ git clone https://github.com/kerryjiang/SuperSock ...

  4. javascirpt中的数字在计算机内存储为多少Byte

    JavaScript内部,所有数字都是以64位浮点数形式储存,即使整数也是如此 8 Byte = 64bit Javascript中,由于其变量内容不同,变量被分为基本数据类型变量和引用数据类型变量. ...

  5. 树莓派+百度api实现人脸识别

    title: 树莓派+百度api实现人脸识别 tags: 树莓派 date: 2018-5-31 20:06:00 --- 树莓派对接百度api 我以前玩安卓的时候一直用的讯飞的平台和api,对于百度 ...

  6. Java线程及Jvm监控工具

    Java线程状态 线程的五种状态 * 新建:new(时间很短) * 运行:runnable * 等待:waitting(无限期等待),timed waitting(限期等待) * 阻塞:blocked ...

  7. [转]最值得拥有的免费Bootstrap后台管理模板

    在PHP开发项目中,后台管理因为面向群体相对比较固定,大部分以实现业务逻辑和功能.使用Bootstrap后台模板可以让后端开发很轻松的就展现给客户一个响应式的后台,节约前端开发的时间.下面PHP程序员 ...

  8. swift protocol 与类继承结合时的bug

    protocol CommonTrait: class { func commonBehavior() -> String } extension CommonTrait { func comm ...

  9. CAD向控件注册一个命令

    _DMxDrawX::RegistUserCustomCommand 向控件注册一个命令,用户在命令行输入命令名这个字符串,就会触发执行命令事件 命令事件的id就是该注册时的id值,成功返回true. ...

  10. xilinx vivado 百度云分享 vivado2019.1 2018.3 2017.4

    vivado2019.1 sdx套件 链接:https://pan.baidu.com/s/1ymRpUa2UYTFuafEChA0-ZQ 提取码:cd4p 复制这段内容后打开百度网盘手机App,操作 ...