시작하는 동안 봄 부팅 응용 프로그램을 실행하는 동안 예외가 발생합니다.
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);
}
}
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>
답변
정확히 오류가 말하는 것입니다. RestTemplate
빈을 생성하지 않았 으므로 자동 배선 할 수 없습니다. 필요한 경우 RestTemplate
제공 해야합니다 . 예를 들어 다음을 TestMicroServiceApplication.java에 추가합니다 .
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
Eureka 용 Spring 클라우드 스타터의 이전 버전에서는 RestTemplate
빈이 생성되었지만 더 이상 사실이 아닙니다.
답변
사용중인 기술과 클래스 RestTemplate
에서 정의하는 방법에 영향을 미치는 버전 에 따라 @Configuration
다릅니다.
Spring> = 4 (Spring Boot없이)
간단히 정의 @Bean
:
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
스프링 부트 <= 1.3
정의 할 필요가 없습니다. Spring Boot는 자동으로 정의합니다.
스프링 부트> = 1.4
Spring Boot는 더 이상 자동으로 정의 RestTemplate
하지 않고 대신 RestTemplateBuilder
생성되는 RestTemplate을 더 많이 제어 할 수 있도록 정의합니다 . 메서드에 RestTemplateBuilder
인수로 를 삽입하여 다음 @Bean
을 생성 할 수 있습니다 RestTemplate
.
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
// Do any additional configuration here
return builder.build();
}
수업에서 사용
@Autowired
private RestTemplate restTemplate;
답변
TestRestTemplate이 단위 테스트에서 유효한 옵션 인 경우이 문서는 관련이있을 수 있습니다.
짧은 대답 : 사용하는 경우
@SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT)
그런 다음 @Autowired
작동합니다. 사용하는 경우
@SpringBootTest(webEnvironment=WebEnvironment.MOCK)
그런 다음 다음과 같이 TestRestTemplate을 만듭니다.
private TestRestTemplate template = new TestRestTemplate();
답변
오류는 RestTemplate
Bean이 컨텍스트에 정의되지 않았으며 Bean을로드 할 수 없음을 직접 가리 킵니다 .
- RestTemplate에 대한 빈을 정의한 다음 사용
- RestTemplate의 새 인스턴스 사용
Bean이 RestTemplate에 대해 정의되어 있다고 확신하는 경우 다음을 사용하여 스프링 부트 애플리케이션에서로드 한 컨텍스트에서 사용 가능한 Bean을 인쇄하십시오.
ApplicationContext ctx = SpringApplication.run(Application.class, args);
String[] beanNames = ctx.getBeanDefinitionNames();
Arrays.sort(beanNames);
for (String beanName : beanNames) {
System.out.println(beanName);
}
여기에 주어진 이름 / 유형의 빈이 포함되어 있으면 모두 좋습니다. 또는 새 빈을 정의한 다음 사용하십시오.
답변
RestTemplate 인스턴스는 종종 사용하기 전에 커스터마이징이 필요하기 때문에 Spring Boot는 단일 자동 구성 RestTemplate 빈을 제공하지 않습니다.
RestTemplateBuilder 는 기본 인증 또는 인터셉터와 같이 나머지 템플릿 빈을 구성하고 인스턴스화하는 적절한 방법을 제공합니다.
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
return builder
.basicAuthorization("user", "name") // Optional Basic auth example
.interceptors(new MyCustomInterceptor()) // Optional Custom interceptors, etc..
.build();
}
답변
- 추가해야합니다
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder){
return builder.build();
}
답변
다음 두 가지를 확인하십시오.
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();
}
}
