Springboot RestTemplate 簡單使用解析
前言
spring框架提供的RestTemplate類可用于在應用中調用rest服務,它簡化了與http服務的通信方式,統(tǒng)一了RESTful的標準,封裝了http鏈接, 我們只需要傳入url及返回值類型即可。
相較于之前常用的HttpClient,RestTemplate是一種更優(yōu)雅的調用RESTful服務的方式。該類主要用到的函數(shù)有:exchange、getForEntity、postForEntity等。我主要用的是后面兩個函數(shù),來執(zhí)行發(fā)送get跟post請求。
首先是RestTemplateUtil類
package cn.eangaie.demo.util; import com.alibaba.fastjson.JSONObject; import org.springframework.http.*; import org.springframework.stereotype.Component; import org.springframework.util.MultiValueMap; import org.springframework.web.client.RestTemplate; import java.util.Map; /** * @author Eangaie * @date 2018/10/12 0012 下午 14:53 * 網絡請求,RestTemplate工具類 */ @Component public class RestTemplateUtil { private RestTemplate restTemplate; /** * 發(fā)送GET請求 * @param url * @param param * @return */ public String GetData(String url, Map<String,String> param){ restTemplate=new RestTemplate(); // 請勿輕易改變此提交方式,大部分的情況下,提交方式都是表單提交 HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); return restTemplate.getForEntity(url,String.class,param).getBody(); } /** * 發(fā)送POST-JSON請求 * @param url * @param param * @return */ public String PostJsonData(String url,JSONObject param){ restTemplate=new RestTemplate(); HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON_UTF8); headers.add("Accept", MediaType.APPLICATION_JSON.toString()); HttpEntity<JSONObject> requestEntity = new HttpEntity<JSONObject>(param, headers); return restTemplate.postForEntity(url,param,String.class).getBody(); } /** * 發(fā)送POST 表單請求 * @param url * @param param * @return */ public String PostFormData(String url,MultiValueMap<String,String> param){ restTemplate=new RestTemplate(); // 請勿輕易改變此提交方式,大部分的情況下,提交方式都是表單提交 HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); return restTemplate.postForEntity(url,param,String.class).getBody(); } }
這里編寫了三個函數(shù),一個是GetData(), 用來發(fā)送GET請求,使用方法是問號傳參,并把參數(shù)的key作為替代,在map中填入。
PostJsonData ()是用來發(fā)送json類型數(shù)據(jù)的POST請求。需要傳入url鏈接,和一個JSONObject對象。PostFormData()函數(shù)是用來發(fā)送表單類型
的POST請求。 使用方式我也編寫了一些簡單的控制器。代碼如下。
@GetMapping("testGetData") public String testGetData(){ String url="http://localhost:81/sample/GetData?msg={msg}&author={author}"; Map<String,String> param=new HashMap<>(); param.put("msg","Hello"); param.put("author","彥杰"); return restTemplateUtil.GetData(url,param); } @GetMapping("testPostJsonData") public String testPostJsonData(){ String url="http://localhost:81/sample/PostData"; JSONObject jsonObject=new JSONObject(); jsonObject.put("msg","hello"); jsonObject.put("author","彥杰"); return restTemplateUtil.PostJsonData(url,jsonObject); } @GetMapping("testPostFormData") public String testPostFormData(){ String url="http://localhost:81/sample/PostFormData"; MultiValueMap<String,String> param=new LinkedMultiValueMap<>(); param.add("msg","Hello"); param.add("author","彥杰"); return restTemplateUtil.PostFormData(url,param); } @GetMapping("GetData") public String getData(String msg, String author){ return msg+" "+author; } @PostMapping("PostData") public String postData(@RequestBody JSONObject jsonObject){ String msg=jsonObject.getString("msg"); String author=jsonObject.getString("author"); return msg+" "+author; } @PostMapping("PostFormData") public String PostFormData(String msg,String author){ return msg+" "+author; } @GetMapping("testGetData") public String testGetData(){ String url="http://localhost:81/sample/GetData?msg={msg}&author={author}"; Map<String,String> param=new HashMap<>(); param.put("msg","Hello"); param.put("author","彥杰"); return restTemplateUtil.GetData(url,param); } @GetMapping("testPostJsonData") public String testPostJsonData(){ String url="http://localhost:81/sample/PostData"; JSONObject jsonObject=new JSONObject(); jsonObject.put("msg","hello"); jsonObject.put("author","彥杰"); return restTemplateUtil.PostJsonData(url,jsonObject); } @GetMapping("testPostFormData") public String testPostFormData(){ String url="http://localhost:81/sample/PostFormData"; MultiValueMap<String,String> param=new LinkedMultiValueMap<>(); param.add("msg","Hello"); param.add("author","彥杰"); return restTemplateUtil.PostFormData(url,param); } @GetMapping("GetData") public String getData(String msg, String author){ return msg+" "+author; } @PostMapping("PostData") public String postData(@RequestBody JSONObject jsonObject){ String msg=jsonObject.getString("msg"); String author=jsonObject.getString("author"); return msg+" "+author; } @PostMapping("PostFormData") public String PostFormData(String msg,String author){ return msg+" "+author; }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- SpringBoot使用RestTemplate實現(xiàn)HTTP請求詳解
- SpringBoot中RestTemplate的使用詳解
- springboot中的RestTemplate使用詳解
- SpringBoot使用RestTemplate的示例詳解
- Springboot使用RestTemplate調用第三方接口的操作代碼
- Springboot之restTemplate的配置及使用方式
- SpringBoot 如何使用RestTemplate發(fā)送Post請求
- 關于springboot 中使用httpclient或RestTemplate做MultipartFile文件跨服務傳輸?shù)膯栴}
- SpringBoot3 RestTemplate配置與使用詳解
相關文章
java數(shù)據(jù)結構算法稀疏數(shù)組示例詳解
這篇文章主要為大家介紹了java數(shù)據(jù)結構算法稀疏數(shù)組示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-06-06Java源碼解析ArrayList及ConcurrentModificationException
今天小編就為大家分享一篇關于Java源碼解析ArrayList及ConcurrentModificationException,小編覺得內容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-01-01詳解在Spring MVC或Spring Boot中使用Filter打印請求參數(shù)問題
這篇文章主要介紹了詳解在Spring MVC或Spring Boot中使用Filter打印請求參數(shù)問題,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-04-04Spring利用@Validated注解實現(xiàn)參數(shù)校驗詳解
這篇文章主要為大家詳細介紹了在?Spring?項目中使用?@Validated?進行參數(shù)校驗的方法和常見應用場景,感興趣的小伙伴可以跟隨小編一起學習一下2023-05-05PowerJob的TimingStrategyHandler工作流程源碼解讀
這篇文章主要為大家介紹了PowerJob的TimingStrategyHandler工作流程源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2024-01-01