Spring 弹簧&x27;RESTS模板bean错误
我有以下配置类(用于定义可以自动连接的RESTTemplatebean): 但是我得到了以下错误:Spring 弹簧&x27;RESTS模板bean错误,spring,spring-mvc,spring-boot,resttemplate,Spring,Spring Mvc,Spring Boot,Resttemplate,我有以下配置类(用于定义可以自动连接的RESTTemplatebean): 但是我得到了以下错误: The class org.springframework.web.client.RestTemplate used in the bean named getRestTemplate is not allowed to use it. 发生了什么事 提前感谢如下配置: @Configuration @ComponentScan(*YOUR PACKAGE NAME*) pub
The class org.springframework.web.client.RestTemplate used in the bean named getRestTemplate is not allowed to use it.
发生了什么事
提前感谢如下配置:
@Configuration
@ComponentScan(*YOUR PACKAGE NAME*)
public class AppConfig {
@Bean
RestTemplate restTemplate() {
RestTemplate restTemplate = new RestTemplate();
MappingJacksonHttpMessageConverter converter = new MappingJacksonHttpMessageConverter();
converter.setObjectMapper(new ObjectMapper());
restTemplate.getMessageConverters().add(converter);
return restTemplate;
}
}
正如Amit K Bist所说,在带有@SprinBootApplication符号的类中,应该定义一个类型为的bean org.springframework.web.client.RestTemplate 我也有同样的问题,但这解决了它,我希望它有帮助
package [package];
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableCaching
@ComponentScan({ [package/s] })
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean(name = "restTemplate")
public RestTemplate restTemplate(RestTemplateBuilder builder) {
RestTemplate REST_TEMPLATE = builder.build(); return REST_TEMPLATE;
}
}
这有什么区别?为什么要
@ComponentScan
解决任何问题?好吧,你复制并粘贴了这个。我的问题是它如何解决当前的问题@ComponentScan
对初始化RestTemplate
bean没有任何作用。感谢您提供信息。您可以显示完整的错误日志吗??顺便说一句,为什么bean不是公共的?你试过这个定义吗:@bean public RestTemplate RestTemplate(RestTemplateBuilder builder){return builder.build();}
是的,它不起作用。当你使用SpringBoot时,你能定义@bean(name=“RestTemplate”)公共RestTemplate RestTemplate吗(RestTemplateBuilder生成器){RestTemplate REST_TEMPLATE=builder.build();返回REST_TEMPLATE;}
在声明了@SpringBootApplication
注释的类中
package [package];
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableCaching
@ComponentScan({ [package/s] })
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean(name = "restTemplate")
public RestTemplate restTemplate(RestTemplateBuilder builder) {
RestTemplate REST_TEMPLATE = builder.build(); return REST_TEMPLATE;
}
}