无法在Spring启动应用程序中自动连接字段:RestTemplate
我在启动期间运行spring boot应用程序时遇到以下异常:无法在Spring启动应用程序中自动连接字段:RestTemplate,spring,maven,spring-boot,resttemplate,Spring,Maven,Spring Boot,Resttemplate,我在启动期间运行spring boot应用程序时遇到以下异常: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'testController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationExc
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中自动连接RestTemplate。我正在使用Maven进行依赖关系管理
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);
}
}
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";
}
}
TestController.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);
}
}
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>
4.0.0
com.micro.test
测试微服务
0.0.1-快照
罐子
测试微服务
SpringBoot的演示项目
org.springframework.boot
spring启动程序父级
1.3.3.1发布
UTF-8
1.8
org.springframework.boot
SpringBootStarterWeb
org.springframework.boot
弹簧起动试验
测试
org.springframework.boot
springbootmaven插件
错误直接指出,restemplate
bean未在上下文中定义,无法加载bean
ApplicationContext ctx = SpringApplication.run(Application.class, args);
String[] beanNames = ctx.getBeanDefinitionNames();
Arrays.sort(beanNames);
for (String beanName : beanNames) {
System.out.println(beanName);
}
如果它包含给定名称/类型的bean,那么一切都很好。或者定义一个新bean,然后使用它。这正是错误所说的。您没有创建任何
restemplate
bean,因此它无法自动关联任何内容。如果需要rest模板
,则必须提供一个。例如,将以下内容添加到TestMicroServiceApplication.java中:
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
请注意,在早期版本的Eureka Spring cloud starter中,为您创建了一个
RestTemplate
bean,但这不再是事实 如果TestRestTemplate是单元测试中的有效选项,则此文档可能与此相关
简短回答:如果使用
@SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT)
然后,@Autowired
将起作用。如果使用
@SpringBootTest(webEnvironment=WebEnvironment.MOCK)
然后创建一个TestRestTemplate,如下所示
private TestRestTemplate template = new TestRestTemplate();
由于RestTemplate实例通常需要在使用之前进行定制,所以SpringBoot不提供任何一个自动配置的RESTTemplatebean
提供了配置和实例化rest模板bean的正确方法,例如基本身份验证或拦截器
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
return builder
.basicAuthorization("user", "name") // Optional Basic auth example
.interceptors(new MyCustomInterceptor()) // Optional Custom interceptors, etc..
.build();
}
取决于您正在使用的技术以及哪些版本将影响您在
@Configuration
类中定义RestTemplate
的方式
弹簧>=4,不带弹簧套
只需定义一个@Bean
:
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
弹簧靴=1.4
Spring Boot不再自动定义RestTemplate
,而是定义了RestTemplateBuilder
,允许您对创建的RestTemplate进行更多控制。您可以将restemplatebuilder
作为参数插入@Bean
方法中,以创建restemplate
:
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
// Do any additional configuration here
return builder.build();
}
在课堂上使用它
@Autowired
private RestTemplate restTemplate;
请确保有两件事: 1-将
@Bean
注释与方法一起使用
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder){
return builder.build();
}
2-此方法的范围应为公共而非私人
完整示例-
@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();
}
}
我能够实现类似壮举的最简单方法是使用下面的代码(), 但我建议不要在controllers()中进行API调用。 此外,这种自动布线方式比传统的方式优化得更好
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.client.RestTemplateBuilder;
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 {
private final RestTemplate restTemplate;
@Autowired
public TestController(RestTemplateBuilder builder) {
this.restTemplate = builder.build();
}
@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";
}
}
- 你必须加上
@Bean 公共RestTemplate RestTemplate(RestTemplateBuilder){ 返回builder.build(); }
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class YourConfigClass {
@Bean
public RestTemplate restTesmplate() {
return new RestTemplate();
}
}
非常感谢您的回复。这有帮助!将问题和答案向上投票,因为当所有其他内容都神奇地为您创建和链接时,您不必手动创建
RestTemplate
。尤其是如果使用SpringCloud之前,它提供了一个自动配置的RestTemplate
;-)老实说,这就是我把这个问题放在论坛上的原因。我希望RestTemplate能为我链接。:-)当我在POM.xml中包含了Eureka依赖项时,它工作得很好。在没有定义RESTTemplatebean的情况下,它工作得很好。Eureka的一个类可能定义了这个bean,只是一个更新。从Spring Boot 1.4.0RestTemplateBuilder
可以用于管理RestTemplate
实例。这里的示例我还不能升级到SB 1.4.0。我想在1.3.8.RELEASE中这样做,但是@g00glen00b解决方案对我不起作用。我还在使用版本为1.1.5.RELEASE
的springcloudnetflix
artifactid。我的RestTemplate是从@RestController
java类调用的,该类使用@Autowired
作为RestTemplate
。有人能帮你吗?投票表决你的问题,因为这是不明显的,当一切都神奇地链接一个restemplate
不是自动为你创建的。投票表决-在Spring Boot自己的页面上的教程没有提到创建一个restemplate Bean!!类似的: