如何使用注释自动连接RestTemplate

如何使用注释自动连接RestTemplate,第1张

如何使用注释自动连接RestTemplate

如果

RestTemplate
定义错误,则会看到错误

考虑在配置中定义类型为“ org.springframework.web.client.RestTemplate”的bean。

要么

找不到类型为[org.springframework.web.client.RestTemplate]的合格Bean

如何定义

RestTemplate
通孔注释

取决于你使用的技术以及哪种版本会影响你RestTemplate在@Configuration课堂上定义a 的方式。

没有Spring Boot的Spring> = 4

只需定义一个

@Bean

@Beanpublic RestTemplate restTemplate() {    return new RestTemplate();}

Spring Boot <= 1.3

无需定义一个,Spring Boot会自动为你定义一个。

Spring Boot >= 1.4

Spring Boot不再自动定义一个

RestTemplate
,而是定义一个
RestTemplateBuilder
允许你对
RestTemplate
创建的对象进行更多控制的控件。你可以
RestTemplateBuilde
r在
@Bean
方法中注入作为参数来创建一个
RestTemplate

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

在课堂上使用它

@Autowiredprivate RestTemplate restTemplate;

or

@Injectprivate RestTemplate restTemplate;


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5021849.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-15
下一篇 2022-11-15

发表评论

登录后才能评论

评论列表(0条)

保存