Depending on what 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;