【发布时间】:2018-03-27 09:52:34
【问题描述】:
我有以下配置类(用于定义可以自动装配的 RestTemplate bean):
@Configuration
public class RestTemplateConfiguration {
@Bean
RestTemplate getRestTemplate() {
return new RestTemplate();
}
}
然后我收到以下错误:
The class org.springframework.web.client.RestTemplate used in the bean named getRestTemplate is not allowed to use it.
发生了什么事?
提前致谢
【问题讨论】:
-
你能显示完整的错误日志吗?顺便说一句,它和我一起工作
-
为什么 bean 不公开?
-
你试过这个定义吗:
@Bean public RestTemplate restTemplate(RestTemplateBuilder builder) {return builder.build(); } -
是的,它也不起作用
-
当您使用SpringBoot时,您可以在声明
@SpringBootApplication注释的类中定义@Bean(name = "restTemplate") public RestTemplate restTemplate(RestTemplateBuilder builder) { RestTemplate REST_TEMPLATE = builder.build(); return REST_TEMPLATE; }
标签: spring spring-mvc spring-boot resttemplate