错误如下:

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

最新文章

  1. JSON总结
  2. OS开发(Objective-C)常用库索引
  3. fiddler 新发现
  4. 一起刷LeetCode3-Longest Substring With Repeating Characters
  5. UVA 100 - The 3n+1 problem (3n+1 问题)
  6. git Bug分支
  7. JavaEE:Tomcat服务器常用配置和HTTP简介
  8. HDU 1407 测试你是否和LTC水平一样高(枚举)
  9. 关于jQuery插件imgAreaSelect基础讲解
  10. 201521123001《Java程序设计》第12周学习总结
  11. html5视频标签
  12. 字典的.get方法
  13. &lt;noip2017&gt;列队
  14. Android APK代码混淆与资源混淆详解,你确定不看?
  15. Cracking The Coding Interview 2.2
  16. Spring boot实现自定义拦截器
  17. POJ1661(KB12-M DP)
  18. 【bzoj1069】最大土地面积
  19. AngularJS + ui-router + RequireJS异步加载注册controller/directive/filter/service
  20. SQL Server复制

热门文章

  1. 大数高精度加减乘除 51nod 1005 大数加法
  2. 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛-B-Perfect Numbers(完数)
  3. .Net MVC 与WebApi ActionFilterAttribute 区别
  4. WordPress个性页面制作教程
  5. Windows下压缩成tar.gz格式
  6. iframe子页面让父页面跳转 parent.location.href
  7. 15Microsoft SQL Server 数据库维护
  8. 牛客多校Round 8
  9. Spring事物不回滚
  10. TWaver MONO模板库新鲜出炉 精彩纷呈