Skip to content

Instantly share code, notes, and snippets.

@RealDeanZhao
Last active May 23, 2023 07:45
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save RealDeanZhao/38821bc1efeb7e2a9bcd554cc06cdf96 to your computer and use it in GitHub Desktop.
Save RealDeanZhao/38821bc1efeb7e2a9bcd554cc06cdf96 to your computer and use it in GitHub Desktop.
How to autowire RestTemplate using annotations

From StackOverflow

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;
@Eveluz
Copy link

Eveluz commented Apr 12, 2019

gracias

@jometho
Copy link

jometho commented Jun 18, 2019

Perfect solution

@preetiagarwal26
Copy link

Thanks. Encountered same issue. Fixed As per your suggestion.

@abdussamad123
Copy link

Thanks for the good solution.

@Mixermachine
Copy link

I followed the example and had the following code in my Client class

    @Autowired
    private RestTemplate restTemplate;

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

During the update from spring 2.3.10 to 2.6.6 Spring now complains that I have a cyclic reference in this same class (worked before).
I had to pull the @bean part out of my original class to get rid of the cyclic reference.
It is now in my SpringConfig class (@configuration annotation).

@ButyAfter
Copy link

image
我是springboot2.2.0的,RestTemplateBuilder builder这个bean还是找不到啊

@dezmans
Copy link

dezmans commented Aug 26, 2022

A similar progression, as Mixermachine reported, happened while updating versions to address some IQ Server security violations caught by our scans.

I had to run from the command line to get the details ==> mvn spring-boot:run , because the app simply would not start from the IDE.
Same thing, first it complained about the @bean in my Controller's code, as circular reference. Then it suggested initialize it in the @configuration class,

This happened when upgrading from SpringBoot 2.5.6 to 2.7.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment