无法在Spring启动应用程序中自动装入字段:RestTemplate

在启动过程中运行弹簧引导应用程序时,出现以下exception:

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)} 

我在我的TestController中自动assemblyRestTemplate。 我正在使用Maven进行依赖pipe理。

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> 

这正是错误所说的。 你没有创build任何RestTemplate bean,所以它不能自动RestTemplate任何东西。 如果你需要一个RestTemplate你必须提供一个。 例如,将以下内容添加到TestMicroServiceApplication.java

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

请注意,在Eureka的Spring云启动器的早期版本中,为您创build了一个RestTemplate bean,但这不再是真实的。

如果TestRestTemplate在unit testing中是一个有效的选项,那么这个文档可能是相关的

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

简短的回答:如果使用

 @SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT) 

那么@Autowired将工作。 如果使用

 @SpringBootTest(webEnvironment=WebEnvironment.MOCK) 

然后像这样创build一个TestRestTemplate

 private TestRestTemplate template = new TestRestTemplate(); 

错误直接指出RestTemplate bean没有在上下文中定义,并且无法加载bean。

  1. 为RestTemplate定义一个bean,然后使用它
  2. 使用RestTemplate的新实例

如果您确定为RestTemplate定义了bean,那么使用以下命令打印spring引导应用程序加载的上下文中可用的bean

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

如果这个包含bean的名字/types给定,那么一切都很好。 或者定义一个新的bean然后使用它。