错误描述:
使用@Autowired 装配RestTemplate时提示:
错误原因
错误提示说RestTemplate没找到,这是因为在 Spring Boot 1.3版本中,会默认提供一个RestTemplate的实例Bean,而在 Spring Boot 1.4以及以后的版本中,这个默认的bean不再提供了,我们需要在Application启动时,手动创建一个RestTemplate的配置。
解决
手动创建RestTemplate实例
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
/**
* @author EDZ
* @Configuration 相当于 spring中的 application.xml
*/
@Configuration
public class ConfigBean {
@Bean
RestTemplate restTemplate(){
return new RestTemplate();
}
}