Springboot RestTemplate 簡單使用解析
前言
spring框架提供的RestTemplate類可用于在應(yīng)用中調(diào)用rest服務(wù),它簡化了與http服務(wù)的通信方式,統(tǒng)一了RESTful的標(biāo)準(zhǔn),封裝了http鏈接, 我們只需要傳入url及返回值類型即可。
相較于之前常用的HttpClient,RestTemplate是一種更優(yōu)雅的調(diào)用RESTful服務(wù)的方式。該類主要用到的函數(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
* 網(wǎng)絡(luò)請求,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;
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- SpringBoot使用RestTemplate實現(xiàn)HTTP請求詳解
- SpringBoot中RestTemplate的使用詳解
- springboot中的RestTemplate使用詳解
- SpringBoot使用RestTemplate的示例詳解
- Springboot使用RestTemplate調(diào)用第三方接口的操作代碼
- Springboot之restTemplate的配置及使用方式
- SpringBoot 如何使用RestTemplate發(fā)送Post請求
- 關(guān)于springboot 中使用httpclient或RestTemplate做MultipartFile文件跨服務(wù)傳輸?shù)膯栴}
- SpringBoot3 RestTemplate配置與使用詳解
相關(guān)文章
Springboot使用pdfbox提取PDF圖片的代碼示例
PDFBox是一個用于創(chuàng)建和處理PDF文檔的Java庫,它可以使用Java代碼創(chuàng)建、讀取、修改和提取PDF文檔中的內(nèi)容,本文就給大家介紹Springboot如何使用pdfbox提取PDF圖片,感興趣的同學(xué)可以借鑒參考2023-06-06
一文解決pom.xml報錯Dependency "xxx" not f
我們在使用maven進(jìn)行jar包管理時有時會遇到pom.xml中報錯Dependency “XXX” not found,所以在本文中將給大家介紹一下pom.xml報錯Dependency "xxx" not found的解決方案,需要的朋友可以參考下2024-01-01
Java8新特性之Base64詳解_動力節(jié)點Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了Java8新特性之Base64的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06
Java將json對象轉(zhuǎn)換為map鍵值對案例詳解
這篇文章主要介紹了Java將json對象轉(zhuǎn)換為map鍵值對案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09
一文帶你搞懂Java中Synchronized和Lock的原理與使用
這篇文章主要為大家詳細(xì)介紹了Java中Synchronized和Lock的原理與使用,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)Java有一定的幫助,需要的可以參考一下2023-04-04
java常用工具類 Reflect反射工具類、String字符串工具類
這篇文章主要為大家詳細(xì)介紹了java常用工具類,包括Reflect反射工具類、String字符串工具類,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-05-05
移動開發(fā)Spring Boot外置tomcat教程及解決方法
這篇文章主要介紹了移動開發(fā)SpringBoot外置tomcat教程,需要的朋友可以參考下2017-11-11

