82

When I try to autowire Spring RestTemplate, I am getting following error:

nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: 
No qualifying bean of type [org.springframework.web.client.RestTemplate] found for dependency: 
expected at least 1 bean which qualifies as autowire candidate for this dependency.

Using Spring 4 in an annotation driven environment.

My dispatcher servlet is configured as follows:

<context:component-scan base-package="in.myproject" />
<mvc:default-servlet-handler />    
<mvc:annotation-driven />
<bean id="restTemplate" class="org.springframework.web.client.RestTemplate"/>

My class in which I am trying to autowire RestTemplate is as follows:

@Service("httpService")
public class HttpServiceImpl implements HttpService {
    
    @Autowired
    private RestTemplate restTemplate;

    @Override
    public void sendUserId(String userId){
   
        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
        map.add("userId", userId);
        map.add("secretKey", "kbhyutu7576465duyfy");
        
        restTemplate.postForObject("http://localhost:8081/api/user", map, null);
     
    }

}
Manuel Jordan
  • 15,253
  • 21
  • 95
  • 158
Meeti Sharma
  • 1,072
  • 1
  • 7
  • 13
  • Can you confirm that in your HttpServiceImpl you are importing the class org.springframework.web.client.RestTemplate and not some other RestTemplate implementation? – ConMan Jan 19 '15 at 12:47
  • I import the same org.springframework.web.client.RestTemplate – Meeti Sharma Jan 19 '15 at 13:19
  • your configuration file is probably not being read, add some other bean there and see if it is registered in the context, if not you got the answer – mariubog Jan 20 '15 at 04:17
  • 1
    I declared bean() in dispatcher-servlet.xml instead of applicationContext.xml now its working fine. – Meeti Sharma Jan 20 '15 at 05:25

6 Answers6

169

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;
dustin.schultz
  • 13,076
  • 7
  • 54
  • 63
  • 1
    the question is about autowiring RestTemplate in a class, so no need to put the bean in `@Configuration` annotated class. It can be put directly in the class (see @eaykin answer) – kiedysktos Jan 05 '18 at 13:37
  • Sure, you can declare it in an `@Component` class (afterall, `@Configuration` is a meta-annotation for `@Component`) but `@Configuration` is preferred. See https://stackoverflow.com/a/28002891/2429176 – dustin.schultz Feb 07 '18 at 00:56
  • Hi, what if my class is an implementation of a functional interface? How to get the restTemplate object autowired? Presence of another function throws an error! – Guru Feb 25 '19 at 08:15
36

You can add the method below to your class for providing a default implementation of RestTemplate:

@Bean
public RestTemplate restTemplate() {
    return new RestTemplate();
}
eaykin
  • 3,713
  • 1
  • 37
  • 33
  • 3
    +1 for mentioning that you can just add it to the class, no need to put it in @Configuration annotated class – kiedysktos Jan 05 '18 at 13:35
  • 2
    I put this in one of my services and it says bean creation circular error, so I need to go with the other way with configuration annotation – Raymond Chen Oct 26 '19 at 21:23
25
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class RestTemplateClient {
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}
Molay
  • 1,154
  • 2
  • 19
  • 42
16

If you're using Spring Boot 1.4.0 or later as the basis of your annotation-driven, Spring doesn't provides a single auto-configured RestTemplate bean. From their documentation:

https://docs.spring.io/spring-boot/docs/1.4.0.RELEASE/reference/html/boot-features-restclient.html

If you need to call remote REST services from your application, you can use Spring Framework’s RestTemplate class. Since RestTemplate instances often need to be customized before being used, Spring Boot does not provide any single auto-configured RestTemplate bean. It does, however, auto-configure a RestTemplateBuilder which can be used to create RestTemplate instances when needed. The auto-configured RestTemplateBuilder will ensure that sensible HttpMessageConverters are applied to RestTemplate instances.

Brice McIver
  • 171
  • 1
  • 4
7

Add the @Configuration annotation in the RestTemplateSOMENAME which extends the RestTemplate class.

@Configuration         
public class RestTemplateClass extends RestTemplate {

}

Then in your controller class you can use the Autowired annotation as follows.

@Autowired   
RestTemplateClass restTemplate;
Mustapha Larhrouch
  • 3,373
  • 3
  • 14
  • 28
  • 1
    This is actually a valid solution if you want to provide multiple rest templates, each configured differently. Although, I'd use `@Component` decoration rather than `@Configuration`, and then provide each implementation as a bean in a `@Configuration` – Jeremy Jan 28 '20 at 18:38
  • 1
    I think it would be better to provide mulitple RestTemplate beans using the RestTemplateBuilder though – Jeremy Jan 28 '20 at 18:50
2
@Autowired
private RestOperations restTemplate;

You can only autowire interfaces with implementations.

hrishikeshp19
  • 8,838
  • 26
  • 78
  • 141
  • RestOperations is an interface http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/client/RestOperations.html – db80 Nov 17 '15 at 10:34
  • Thats what he said. If interface has one implementation you can autowire it. – lukaszrys Nov 08 '16 at 15:50
  • Are you sure it works as is? I get this error: The injection point has the following annotations: - @org.springframework.beans.factory.annotation.Autowired(required=true) Action: Consider defining a bean of type 'org.springframework.web.client.RestOperations' in your configurati – user2918640 Nov 27 '19 at 11:06