Spring Cloud ZooKeeper集成Feign的坑1,错误:Consider defining a bean of type 'org.springframework.web.client.RestTemplate' in your configuration.
错误如下:
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,当然,可以直接在别的地方注入红色部分代码即可。而且,如果哪个组件上注解了这个方法,其余都可以不用,只是一次注解即可。
解释说明:
如果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.的更多相关文章
- 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 ...
- Spring Cloud ZooKeeper集成Feign的坑3,程序Run模式运行没事,Debug模式下报错
请更新Spring Cloud的版本: <dependency> <groupId>org.springframework.cloud</groupId> < ...
- spring cloud gateway网关启动报错:No qualifying bean of type 'org.springframework.web.reactive.DispatcherHandler'
网关配置好后启动报错如下: org.springframework.context.ApplicationContextException: Unable to start web server; n ...
- 【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 ...
- 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 ...
- Spring Cloud(Dalston.SR5)--Feign 声明式REST客户端
Spring Cloud 对 Feign 进行了封装,集成了 Ribbon 并结合 Eureka 可以实现客户端的负载均衡,Spring Cloud 实现的 Feign 客户端类名为 LoadBala ...
- Spring Cloud 入门 之 Feign 篇(三)
原文地址:Spring Cloud 入门 之 Feign 篇(三) 博客地址:http://www.extlight.com 一.前言 在上一篇文章<Spring Cloud 入门 之 Ribb ...
- 0.9.0.RELEASE版本的spring cloud alibaba sentinel+feign降级处理实例
既然用到了feign,那么主要是针对服务消费方的降级处理.我们基于0.9.0.RELEASE版本的spring cloud alibaba nacos+feign实例添油加醋,把sentinel功能加 ...
- 解决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 ...
随机推荐
- springmvc中的web.xml配置(包含中文乱码解决)
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http:// ...
- C#基础 集合
//数组定义的时候 //需要定义数据类型 //需要定义初始长度 //int [] array = new int[5]; //int a = array.Length; //集合 //ArrayLis ...
- [Windows Server 2012] 更换PHP版本方法
★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com ★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频. ★ 本节我们将带领大家:更换PHP ...
- JAVA 学习笔记 - 基础语法1
1. 类的定义 public class 与 class的区别 一个java文件只有一个public class, 而且类名必须与文件名一致. 一个java文件可以有多个class定义,javac ...
- HDU_2955_Robberies_01背包
A - Robberies Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submi ...
- Java实现打包文件
把文件打包到压缩包里 public void zip (String... files) throws IOException { //创建文件打包流对象 ZipOutputStream zos = ...
- vue之基础---组件基础
(1)基本示例 Vue组件示例 /* 先注册组件,定义一个名为button-component的新组件 */ Vue.component('button-component',{ data:funct ...
- 一天搞定jQuery(三)——使用jQuery完成复选框的全选和全不选
还记得之前我使用JavaScript来实现复选框的全选和全不选效果吗?如果读者初次翻阅本文,可记得看看教你一天玩转JavaScript(七)——使用JavaScript完成复选框的全选和全不选的效果! ...
- docker 1-->docker swarm 转载
实践中会发现,生产环境中使用单个 Docker 节点是远远不够的,搭建 Docker 集群势在必行.然而,面对 Kubernetes, Mesos 以及 Swarm 等众多容器集群系统,我们该如何选择 ...
- typora_test
加粗标题 加下标线 <!--aba--> #Include  ![](http://gyz.g ...