欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Springboot RestTemplate 簡單使用解析

 更新時間:2019年08月08日 10:05:41   作者:謝彥杰  
這篇文章主要介紹了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;
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • 淺談利用Session防止表單重復提交

    淺談利用Session防止表單重復提交

    這篇文章主要介紹了淺談利用Session防止表單重復提交,簡單介紹表單重復提交的情況,分析,以及解決方法代碼示例,具有一定借鑒價值,需要的朋友可以了解下。
    2017-12-12
  • java數(shù)據(jù)結構算法稀疏數(shù)組示例詳解

    java數(shù)據(jù)結構算法稀疏數(shù)組示例詳解

    這篇文章主要為大家介紹了java數(shù)據(jù)結構算法稀疏數(shù)組示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-06-06
  • java中的session對象及其常用方法小結

    java中的session對象及其常用方法小結

    這篇文章主要介紹了java中的session對象及其常用方法小結,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • Spring中校驗器(Validator)的深入講解

    Spring中校驗器(Validator)的深入講解

    Spring校驗器,參數(shù)校驗從此簡單。下面這篇文章主要給大家介紹了關于Spring中校驗器(Validator)的相關資料,文中通過示例代碼介紹非常詳細,需要的朋友可以參考借鑒,下面隨著小編來一起學習學習吧
    2018-06-06
  • Java源碼解析ArrayList及ConcurrentModificationException

    Java源碼解析ArrayList及ConcurrentModificationException

    今天小編就為大家分享一篇關于Java源碼解析ArrayList及ConcurrentModificationException,小編覺得內容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • Java泛型類型通配符和C#對比分析

    Java泛型類型通配符和C#對比分析

    下面小編就為大家?guī)硪黄狫ava泛型類型通配符和C#對比分析。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-10-10
  • java雙重檢查鎖定的實現(xiàn)代碼

    java雙重檢查鎖定的實現(xiàn)代碼

    這篇文章主要介紹了java雙重檢查鎖定的實現(xiàn)方法,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-06-06
  • 詳解在Spring MVC或Spring Boot中使用Filter打印請求參數(shù)問題

    詳解在Spring MVC或Spring Boot中使用Filter打印請求參數(shù)問題

    這篇文章主要介紹了詳解在Spring MVC或Spring Boot中使用Filter打印請求參數(shù)問題,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-04-04
  • Spring利用@Validated注解實現(xiàn)參數(shù)校驗詳解

    Spring利用@Validated注解實現(xiàn)參數(shù)校驗詳解

    這篇文章主要為大家詳細介紹了在?Spring?項目中使用?@Validated?進行參數(shù)校驗的方法和常見應用場景,感興趣的小伙伴可以跟隨小編一起學習一下
    2023-05-05
  • PowerJob的TimingStrategyHandler工作流程源碼解讀

    PowerJob的TimingStrategyHandler工作流程源碼解讀

    這篇文章主要為大家介紹了PowerJob的TimingStrategyHandler工作流程源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2024-01-01

最新評論