如何使用注释自动装配RestTemplate

问题描述 投票:0回答:6

当我尝试自动装配 Spring

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.

在注释驱动的环境中使用 Spring 4。

我的调度程序 servlet 配置如下:

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

}
java spring spring-mvc resttemplate
6个回答
169
投票

如果未定义

RestTemplate
,您会看到错误

考虑定义一个类型的bean 配置中的“org.springframework.web.client.RestTemplate”。

没有合格的 bean 类型 [org.springframework.web.client.RestTemplate] 找到

如何通过注释定义

RestTemplate

取决于您使用的技术和版本将影响您在

RestTemplate
类中定义
@Configuration
的方式。

Spring >= 1.4,没有 Spring Boot

简单地定义一个

@Bean
:

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

Spring Boot <= 1.3

无需定义,Spring Boot 会自动为您定义。

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;

@Inject
private RestTemplate restTemplate;

36
投票

您可以将以下方法添加到您的类中以提供 RestTemplate 的默认实现:

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

25
投票
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();
    }
}

16
投票

如果您使用 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 将确保将合理的 HttpMessageConverters 应用于 RestTemplate 实例。


7
投票

在 RestTemplateSOMENAME 中添加

@Configuration
注释,它扩展了 RestTemplate 类。

@Configuration         
public class RestTemplateClass extends RestTemplate {

}

然后在您的控制器类中,您可以使用 Autowired 注释,如下所示。

@Autowired   
RestTemplateClass restTemplate;

2
投票
@Autowired
private RestOperations restTemplate;

您只能通过实现来自动装配接口。

© www.soinside.com 2019 - 2024. All rights reserved.