SpringBoot RestTemplate 簡單包裝解析
RestTemplate設(shè)計是為了Spring更好的請求并解析Restful風(fēng)格的接口返回值而設(shè)計的,通過這個類可以在請求接口時直接解析對應(yīng)的類。
在SpringBoot中對這個類進行簡單的包裝,變成一個工具類來使用,這里用到的是getForEntity和postForEntity方法,具體包裝的代碼內(nè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ò)請求,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();
}
}
在控制類里面調(diào)用函數(shù),看看效果
@GetMapping("selectUser")
public Result selectUser(int id){
user=userService.selectById(id);
return ResultUtil.success(user,"查詢成功");
}
@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í)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java將時間戳轉(zhuǎn)換為Date對象的方法小結(jié)
在?Java?編程中,處理日期和時間是一個常見需求,特別是在處理網(wǎng)絡(luò)通信或者數(shù)據(jù)庫操作時,本文主要為大家整理了Java中將時間戳轉(zhuǎn)換為Date對象的方法,希望對大家有所幫助2024-12-12
基于Spring Boot的Logback日志輪轉(zhuǎn)配置詳解
本篇文章主要介紹了基于Spring Boot的Logback日志輪轉(zhuǎn)配置詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10
一次排查@CacheEvict注解失效的經(jīng)歷及解決
這篇文章主要介紹了一次排查@CacheEvict注解失效的經(jīng)歷及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12
Spring Cloud Gateway內(nèi)置的斷言和過濾器作用說明
這篇文章主要介紹了Spring Cloud Gateway內(nèi)置的斷言和過濾器作用說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-06-06
Java后端服務(wù)間歇性響應(yīng)慢的問題排查與解決
之前在公司內(nèi)其它團隊找到幫忙排查的一個后端服務(wù)連接超時問題,問題的表現(xiàn)是服務(wù)部署到線上后出現(xiàn)間歇性請求響應(yīng)非常慢(大于10s),但是后端業(yè)務(wù)分析業(yè)務(wù)日志時卻沒有發(fā)現(xiàn)慢請求,所以本文給大家介紹了Java后端服務(wù)間歇性響應(yīng)慢的問題排查與解決,需要的朋友可以參考下2025-03-03

