Failed to create autwire: RestTemplate field in Spring boot application

During startup, when loading the spring application during startup, the following exception is thrown:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'testController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.web.client.RestTemplate com.micro.test.controller.TestController.restTemplate; 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. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} 

I run RestTemplate in my TestController. I use Maven for dependency management.

TestMicroServiceApplication.java

 package com.micro.test; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class TestMicroServiceApplication { public static void main(String[] args) { SpringApplication.run(TestMicroServiceApplication.class, args); } } 

TestController.java

  package com.micro.test.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @RestController public class TestController { @Autowired private RestTemplate restTemplate; @RequestMapping(value="/micro/order/{id}", method=RequestMethod.GET, produces=MediaType.ALL_VALUE) public String placeOrder(@PathVariable("id") int customerId){ System.out.println("Hit ===> PlaceOrder"); Object[] customerJson = restTemplate.getForObject("http://localhost:8080/micro/customers", Object[].class); System.out.println(customerJson.toString()); return "false"; } } 

pom.xml

  <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.micro.test</groupId> <artifactId>Test-MicroService</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>Test-MicroService</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> 
+73
spring spring-boot maven resttemplate
Mar 22 '16 at 10:08
source share
6 answers

This is exactly what the error says. You have not created a single RestTemplate bean, so it cannot auto-detect. If you need a RestTemplate , you will need to provide one. For example, add the following to TestMicroServiceApplication.java :

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

Please note that in earlier versions of Spring cloud starter for Eureka, a RestTemplate bean was created for you, but this is no longer the case.

+110
Mar 22 '16 at 10:22
source share
— -

Depending on which technologies you use and which versions will affect how you define RestTemplate in your @Configuration class.

Spring> = 4 without Spring Boot

Just define @Bean :

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

Spring boot <= 1.3

There is no need to define it, Spring Boot automatically detects it 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 created RestTemplate. You can RestTemplateBuilder as an argument to your @Bean method to create a RestTemplate :

 @Bean public RestTemplate restTemplate(RestTemplateBuilder builder) { // Do any additional configuration here return builder.build(); } 

Using it in class

 @Autowired private RestTemplate restTemplate; 

Link

+13
Feb 07 '18 at 3:33
source share

If TestRestTemplate is a valid parameter in your unit test, this documentation may be relevant.

http://docs.spring.io/spring-boot/docs/1.4.1.RELEASE/reference/htmlsingle/#boot-features-rest-templates-test-utility

Short answer: when using

 @SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT) 

then @Autowired will work. Using

 @SpringBootTest(webEnvironment=WebEnvironment.MOCK) 

then create a TestRestTemplate like this

 private TestRestTemplate template = new TestRestTemplate(); 
+7
Oct. 06 '16 at 3:43 on
source share

The error indicates that the RestTemplate bean is not defined in the context and cannot load beans.

  • Define a bean for RestTemplate and then use
  • Use a new instance of RestTemplate

If you are sure that a bean is defined for the RestTemplate template, print beans that are available in the context downloaded by spring to download

use the following:
 ApplicationContext ctx = SpringApplication.run(Application.class, args); String[] beanNames = ctx.getBeanDefinitionNames(); Arrays.sort(beanNames); for (String beanName : beanNames) { System.out.println(beanName); } 

If it contains a bean by name / type, then all is well. Or else define a new bean and then use it.

+1
Mar 22 '16 at 10:22
source share

Since RestTemplate instances often need to be configured before use, Spring Boot does not provide any automatic RestTemplate bean configuration.

RestTemplateBuilder offers the right way to configure and create a bean break pattern, for example for basic auth or interceptors.

 @Bean public RestTemplate restTemplate(RestTemplateBuilder builder) { return builder .basicAuthorization("user", "name") // Optional Basic auth example .interceptors(new MyCustomInterceptor()) // Optional Custom interceptors, etc.. .build(); } 
+1
Nov 29 '17 at 12:22
source share

Please make sure two things:

1- Use the @Bean annotation with the method.

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

2- The scope of this method should be public, not private .

Full example -

 @Service public class MakeHttpsCallImpl implements MakeHttpsCall { @Autowired private RestTemplate restTemplate; @Override public String makeHttpsCall() { return restTemplate.getForObject("https://localhost:8085/onewayssl/v1/test",String.class); } @Bean public RestTemplate restTemplate(RestTemplateBuilder builder){ return builder.build(); } } 
0
Jun 16 '19 at 9:14
source share



All Articles