解決RestTemplate加@Autowired注入不了的問(wèn)題
RestTemplate加@Autowired注入不了
1、在啟動(dòng)類加入
如圖箭頭所示代碼:
然后在進(jìn)行@Autowired發(fā)現(xiàn)不報(bào)錯(cuò)了。
完美解決
SpringBoot 如何注入RestTemplate
創(chuàng)建一個(gè)文件夾 ,我這邊習(xí)慣于創(chuàng)建config文件夾
將下面的一段代碼放到里面
import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.web.client.RestTemplate; @Configuration public class RedisConfig { @Bean public RestTemplate restTemplate(RestTemplateBuilder builder) { RestTemplate restTemplate = builder.build(); restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter()); return restTemplate; } }
之后使用
@Autowired private RestTemplate restTemplate;
直接正常使用就可以
String url = "http://localhost:8080/findById?id=1";//請(qǐng)求的地址 String request = restTemplate.getForObject(url, String.class);
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- SpringBoot-RestTemplate如何實(shí)現(xiàn)調(diào)用第三方API
- SpringBoot 利用RestTemplate http測(cè)試
- RestTemplate 401 獲取錯(cuò)誤信息的處理方案
- 解決RestTemplate 請(qǐng)求接收自定義400+ 或500+錯(cuò)誤
- RestTemplate自定義ErrorHandler方式
- 解決使用RestTemplate時(shí)報(bào)錯(cuò)RestClientException的問(wèn)題
- 解決RestTemplate 的getForEntity調(diào)用接口亂碼的問(wèn)題
- RestTemplate未使用線程池問(wèn)題的解決方法
相關(guān)文章
Java?C++題解leetcode消失的兩個(gè)數(shù)字實(shí)例
這篇文章主要介紹了Java?C++題解leetcode消失的兩個(gè)數(shù)字實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09Java鎖升級(jí)的實(shí)現(xiàn)過(guò)程
這篇文章主要介紹了Java鎖升級(jí)的實(shí)現(xiàn)過(guò)程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05Spring 注解編程模型相關(guān)知識(shí)詳解
這篇文章主要介紹了Spring 注解編程模型相關(guān)知識(shí)詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09java8使用流的filter來(lái)篩選數(shù)據(jù)的實(shí)現(xiàn)
這篇文章主要介紹了java8使用流的filter來(lái)篩選數(shù)據(jù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03SpringBoot實(shí)現(xiàn)調(diào)用自定義的應(yīng)用程序((最新推薦)
這篇文章主要介紹了SpringBoot實(shí)現(xiàn)調(diào)用自定義的應(yīng)用程序的相關(guān)知識(shí),本文給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-06-06淺談在Java中使用Callable、Future進(jìn)行并行編程
這篇文章主要介紹了淺談在Java中使用Callable、Future進(jìn)行并行編程,具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-12-12