Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to autowire RestTemplate using annotations

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);       } } 
like image 738
Meeti Sharma Avatar asked Jan 19 '15 12:01

Meeti Sharma


People also ask

Can we Autowire Rest template?

The RestTemplate cannot be auto-wired without specifying the bean creation configuration. Spring boot can not find the RestTemplate as it could not be found in the loaded bean. If you autowire RestTemplate using annotations without the bean creation configuration, the error required a bean of type 'org.

How do you inject a RestTemplate?

How can I inject this class ? Define an @Bean - annotated method in your configuration class, which creates and returns the RestTemplate. docs.spring.io/spring/docs/current/spring-framework-reference/… Check out the code on this question for exactly what you're trying to do: stackoverflow.com/questions/40473146/…

What is @bean annotation in Spring boot?

Spring @Bean Annotation is applied on a method to specify that it returns a bean to be managed by Spring context. Spring Bean annotation is usually declared in Configuration classes methods. In this case, bean methods may reference other @Bean methods in the same class by calling them directly.

How do you use RestTemplate getForObject?

The getForObject method fetches the data for the given response type from the given URI or URL template using HTTP GET method. To fetch data for the given key properties from URL template we can pass Object Varargs and Map to getForObject method. The getForObject returns directly the object of given response type.


1 Answers

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; 
like image 63
dustin.schultz Avatar answered Oct 07 '22 03:10

dustin.schultz