Errors
you'll see if a RestTemplate isn't
defined
Consider defining a bean of type 'org.springframework.web.client.RestTemplate' in your configuration.
or
No qualifying bean of type [org.springframework.web.client.RestTemplate] found
How
to define a RestTemplate via
annotations
Depending on which technologies you're using and what versions will influence how you define a RestTemplate in
your @Configuration class.
Spring >= 4 without Spring Boot
Simply define an @Bean:
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
Spring Boot <= 1.3
No need to define one, Spring Boot automatically defines one for you.
Spring Boot >= 1.4
Spring Boot no longer automatically defines a RestTemplate but
instead defines a RestTemplateBuilder allowing
you more control over the RestTemplate that
gets created. You can inject the RestTemplateBuilder as
an argument in your @Bean method
to create a RestTemplate:
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
// Do any additional configuration here
return builder.build();
}
Using it in your class
@Autowired
private RestTemplate restTemplate;
or
@Inject
private RestTemplate restTemplate;
本文介绍了在不同Spring版本中如何配置RestTemplate,包括Spring 4及以上版本(不包含Spring Boot)、Spring Boot 1.3及以下版本自动配置的情况,以及Spring Boot 1.4及以上版本通过RestTemplateBuilder进行更细粒度配置的方法。
942

被折叠的 条评论
为什么被折叠?



