Springboot RestTemplate 簡(jiǎn)單使用解析
前言
spring框架提供的RestTemplate類(lèi)可用于在應(yīng)用中調(diào)用rest服務(wù),它簡(jiǎn)化了與http服務(wù)的通信方式,統(tǒng)一了RESTful的標(biāo)準(zhǔn),封裝了http鏈接, 我們只需要傳入url及返回值類(lèi)型即可。
相較于之前常用的HttpClient,RestTemplate是一種更優(yōu)雅的調(diào)用RESTful服務(wù)的方式。該類(lèi)主要用到的函數(shù)有:exchange、getForEntity、postForEntity等。我主要用的是后面兩個(gè)函數(shù),來(lái)執(zhí)行發(fā)送get跟post請(qǐng)求。
首先是RestTemplateUtil類(lèi)
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
* 網(wǎng)絡(luò)請(qǐng)求,RestTemplate工具類(lèi)
*/
@Component
public class RestTemplateUtil {
private RestTemplate restTemplate;
/**
* 發(fā)送GET請(qǐng)求
* @param url
* @param param
* @return
*/
public String GetData(String url, Map<String,String> param){
restTemplate=new RestTemplate();
// 請(qǐng)勿輕易改變此提交方式,大部分的情況下,提交方式都是表單提交
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
return restTemplate.getForEntity(url,String.class,param).getBody();
}
/**
* 發(fā)送POST-JSON請(qǐng)求
* @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 表單請(qǐng)求
* @param url
* @param param
* @return
*/
public String PostFormData(String url,MultiValueMap<String,String> param){
restTemplate=new RestTemplate();
// 請(qǐng)勿輕易改變此提交方式,大部分的情況下,提交方式都是表單提交
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
return restTemplate.postForEntity(url,param,String.class).getBody();
}
}
這里編寫(xiě)了三個(gè)函數(shù),一個(gè)是GetData(), 用來(lái)發(fā)送GET請(qǐng)求,使用方法是問(wèn)號(hào)傳參,并把參數(shù)的key作為替代,在map中填入。
PostJsonData ()是用來(lái)發(fā)送json類(lèi)型數(shù)據(jù)的POST請(qǐng)求。需要傳入url鏈接,和一個(gè)JSONObject對(duì)象。PostFormData()函數(shù)是用來(lái)發(fā)送表單類(lèi)型
的POST請(qǐng)求。 使用方式我也編寫(xiě)了一些簡(jiǎn)單的控制器。代碼如下。
@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;
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- SpringBoot使用RestTemplate實(shí)現(xiàn)HTTP請(qǐng)求詳解
- SpringBoot中RestTemplate的使用詳解
- springboot中的RestTemplate使用詳解
- SpringBoot使用RestTemplate的示例詳解
- Springboot使用RestTemplate調(diào)用第三方接口的操作代碼
- Springboot之restTemplate的配置及使用方式
- SpringBoot 如何使用RestTemplate發(fā)送Post請(qǐng)求
- 關(guān)于springboot 中使用httpclient或RestTemplate做MultipartFile文件跨服務(wù)傳輸?shù)膯?wèn)題
- SpringBoot3 RestTemplate配置與使用詳解
相關(guān)文章
java數(shù)據(jù)結(jié)構(gòu)算法稀疏數(shù)組示例詳解
這篇文章主要為大家介紹了java數(shù)據(jù)結(jié)構(gòu)算法稀疏數(shù)組示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
java中的session對(duì)象及其常用方法小結(jié)
這篇文章主要介紹了java中的session對(duì)象及其常用方法小結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
Spring中校驗(yàn)器(Validator)的深入講解
Spring校驗(yàn)器,參數(shù)校驗(yàn)從此簡(jiǎn)單。下面這篇文章主要給大家介紹了關(guān)于Spring中校驗(yàn)器(Validator)的相關(guān)資料,文中通過(guò)示例代碼介紹非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-06-06
Java源碼解析ArrayList及ConcurrentModificationException
今天小編就為大家分享一篇關(guān)于Java源碼解析ArrayList及ConcurrentModificationException,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-01-01
詳解在Spring MVC或Spring Boot中使用Filter打印請(qǐng)求參數(shù)問(wèn)題
這篇文章主要介紹了詳解在Spring MVC或Spring Boot中使用Filter打印請(qǐng)求參數(shù)問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
Spring利用@Validated注解實(shí)現(xiàn)參數(shù)校驗(yàn)詳解
這篇文章主要為大家詳細(xì)介紹了在?Spring?項(xiàng)目中使用?@Validated?進(jìn)行參數(shù)校驗(yàn)的方法和常見(jiàn)應(yīng)用場(chǎng)景,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-05-05
PowerJob的TimingStrategyHandler工作流程源碼解讀
這篇文章主要為大家介紹了PowerJob的TimingStrategyHandler工作流程源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01

