Could not autowire field:RestTemplate in Spring boot application

It’s exactly what the error says. You didn’t create any RestTemplate bean, so it can’t autowire any. If you need a RestTemplate you’ll have to provide one. For example, add the following to TestMicroServiceApplication.java:

@Bean
public RestTemplate restTemplate() {
    return new RestTemplate();
}

Since Spring boot 1.4, there’s also a convenient builder that you can autowire and use to create a RestTemplate bean. The benefit is that you can use the builder pattern to register interceptors, customizers, … .

@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
    return builder.build();
}

Note, in earlier versions of the Spring cloud starter for Eureka, a RestTemplate bean was created for you, but this is no longer true.

Leave a Comment