주석을 사용하여 RestTemplate를 자동 배선하는 방법
Spring Rest Template 자동 배선을 시도하면 다음 오류가 나타납니다.
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.
주석 기반 환경에서 스프링 4를 사용합니다.
디스패처 서블릿은 다음과 같이 설정됩니다.
<context:component-scan base-package="in.myproject" />
<mvc:default-servlet-handler />
<mvc:annotation-driven />
<bean id="restTemplate" class="org.springframework.web.client.RestTemplate"/>
RestTemplate를 자동 연결하려는 클래스는 다음과 같습니다.
@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);
}
}
에러:RestTemplate정의되어 있지 않다
org.springframework 유형의 빈을 정의하는 것을 검토합니다.web.client.RestTemplate'를 선택합니다.
또는
[ or . spring framework ]타입의 정규 콩이 없습니다.web.client.RestTemplate]를 찾았습니다.
정의 방법RestTemplate주석을 통해
사용하고 있는 테크놀로지와 어떤 버전이 이 테크놀로지의 정의 방법에 영향을 미치느냐에 따라RestTemplate당신의 안에서@Configuration학급.
스프링 > = 4 (스프링 부트 없음)
간단히 정의하면@Bean:
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
스프링 부트 <= 1.3
정의할 필요가 없습니다.스프링 부트에서는 자동으로 정의됩니다.
스프링 부트 > = 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;
또는
@Inject
private RestTemplate restTemplate;
다음 메서드를 클래스에 추가하여 RestTemplate 기본 구현을 제공할 수 있습니다.
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class RestTemplateClient {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
Spring Boot 1.4.0 이후를 주석 주도의 기반으로 사용하는 경우 Spring에서는 단일 자동 구성 RestTemplate bean이 제공되지 않습니다.문서 내용:
https://docs.spring.io/spring-boot/docs/1.4.0.RELEASE/reference/html/boot-features-restclient.html
응용 프로그램에서 원격 REST 서비스를 호출해야 하는 경우 Spring Framework의 RestTemplate 클래스를 사용할 수 있습니다.RestTemplate 인스턴스를 사용하기 전에 커스터마이즈해야 하는 경우가 많기 때문에 Spring Boot에서는 자동 설정된 RestTemplate bean은 제공되지 않습니다.그러나 RestTemplateBuilder는 필요에 따라 RestTemplate 인스턴스를 만드는 데 사용할 수 있습니다.자동 설정된 RestTemplateBuilder를 사용하면 RestTemplate 인스턴스에 sense HttpMessageConverters가 확실하게 적용됩니다.
를 추가합니다.@ConfigurationRestTemplate 주석RestTemplate 클래스를 확장하는 SOMENAME.
@Configuration
public class RestTemplateClass extends RestTemplate {
}
그런 다음 컨트롤러 클래스에서 다음과 같이 자동 배선 주석을 사용할 수 있습니다.
@Autowired
RestTemplateClass restTemplate;
@Autowired
private RestOperations restTemplate;
자동 접속할 수 있는 것은, 실장과의 인터페이스 뿐입니다.
언급URL : https://stackoverflow.com/questions/28024942/how-to-autowire-resttemplate-using-annotations
'programing' 카테고리의 다른 글
| 사용자가 입력을 완료한 경우에만 angularjs ngChange 핸들러를 호출하려면 어떻게 해야 합니까? (0) | 2023.04.02 |
|---|---|
| Angular.copy() 또는 _.clone() 중 어느 쪽을 사용해야 합니까? (0) | 2023.04.02 |
| UI 라우터를 사용하여 개체를 상태로 전환하려면 어떻게 해야 합니까? (0) | 2023.04.02 |
| WordPress에서 테마 이름을 변경할 수 있습니까? (0) | 2023.04.02 |
| application.properties의 값을 정적 변수에 할당하려면 어떻게 해야 합니까? (0) | 2023.04.02 |