溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何在Springboot中使用RestTemplate

發(fā)布時間:2021-03-24 15:35:36 來源:億速云 閱讀:229 作者:Leah 欄目:編程語言

這期內(nèi)容當中小編將會給大家?guī)碛嘘P(guān)如何在Springboot中使用RestTemplate,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

首先是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;
}

上述就是小編為大家分享的如何在Springboot中使用RestTemplate了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI