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

SpringBoot使用RestTemplate的示例詳解

 更新時間:2023年05月29日 14:55:46   作者:java技術(shù)媛  
RestTemplate繼承自InterceptingHttpAccessor并且實現(xiàn)了RestOperations接口,其中RestOperations接口定義了基本的RESTful操作,這些操作在RestTemplate中都得到了實現(xiàn),這篇文章主要介紹了SpringBoot使用RestTemplate,需要的朋友可以參考下

RestTemplate 是由 Spring 提供的一個 HTTP 請求工具,它提供了常見的REST請求方案的模版,例如 GET 請求、POST 請求、PUT 請求、DELETE 請求以及一些通用的請求執(zhí)行方法 exchange 以及 execute。RestTemplate 繼承自 InterceptingHttpAccessor 并且實現(xiàn)了 RestOperations 接口,其中 RestOperations 接口定義了基本的 RESTful 操作,這些操作在 RestTemplate 中都得到了實現(xiàn)。

POST請求

postForObject

1、使用LinkedMultiValueMap作為參數(shù)(Form表單提交)

RestTemplate template = new RestTemplate();
String url = "http://127.0.0.1:8800/product/update";
MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<String, Object>();
paramMap.add("id", "123");
paramMap.add("name", "張三");
String result = template.postForObject(url, paramMap, String.class);
System.out.println("result:" + result);

2、使用Object作為參數(shù)(JSON提交)

RestTemplate template = new RestTemplate();
String url = "http://127.0.0.1:8800/product/update";
User user = new User(123, "張三");
String result = template.postForObject(url, user, String.class);
System.out.println("result:" + result);

3、使用JSONObject作為參數(shù)(JSON提交)

RestTemplate template = new RestTemplate();
String url = "http://127.0.0.1:8800/product/update";
JSONObject obj = new JSONObject();
obj.put("id", "123");
obj.put("name", "張三");
String result = template.postForObject(url, obj, String.class);
System.out.println("result:" + result);

postForEntity

1、使用LinkedMultiValueMap作為參數(shù)(Form表單提交)

RestTemplate template = new RestTemplate();
String url = "http://127.0.0.1:8800/product/update";
HttpHeaders headers = new HttpHeaders();
headers.set("token", "asdf");
MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<String, Object>();
paramMap.add("id", "123");
paramMap.add("name", "張三");
HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<MultiValueMap<String, Object>>(paramMap, headers);
ResponseEntity<String> response = template.postForEntity(url, httpEntity, String.class);
System.out.println("result:" + response.getBody());

2、使用Object作為參數(shù)(JSON提交)

RestTemplate template = new RestTemplate();
String url = "http://127.0.0.1:8800/product/update";
HttpHeaders headers = new HttpHeaders();
User user = new User(123, "張三");
HttpEntity<User> httpEntity = new HttpEntity<User>(user, headers);
ResponseEntity<String> response = template.postForEntity(url, httpEntity, String.class);
System.out.println("result:" + response.getBody());

3、使用JSONObject為參數(shù)(JSON提交)

RestTemplate template = new RestTemplate();
String url = "http://127.0.0.1:8800/product/update";
HttpHeaders headers = new HttpHeaders();
JSONObject obj = new JSONObject();
obj.put("id", "123");
obj.put("name", "張三");
HttpEntity<JSONObject> httpEntity = new HttpEntity<JSONObject>(obj, headers);
ResponseEntity<String> response = template.postForEntity(url, httpEntity, String.class);
System.out.println("result:" + response.getBody());

exchange

RestTemplate template = new RestTemplate();
String url = "http://127.0.0.1:8800/product/productDetail";
HttpHeaders headers = new HttpHeaders();
MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<String, Object>();
paramMap.add("id", "123");
HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<MultiValueMap<String, Object>>(paramMap, headers);
ResponseEntity<String> response = template.exchange(url, HttpMethod.POST, httpEntity, String.class);
System.out.println("result:" + response.getBody());

postForObject和postForEntity方法的區(qū)別主要在于可以在postForEntity方法中設置header的屬性,當需要指定header的屬性值的時候,使用postForEntity方法。

exchange方法和postForEntity類似,但是更靈活,exchange還可以調(diào)用get、put、delete請求。

GET請求

getForObject

RestTemplate template = new RestTemplate();
String url = "http://127.0.0.1:8800/product/detail?id={id}";
Map<String, Object> paramMap = new HashMap<String, Object>();
paramMap.put("id", "123");
String result = template.getForObject(url, String.class, paramMap);
System.out.println("result:" + result);

getForEntity

RestTemplate template = new RestTemplate();
String url = "http://127.0.0.1:8800/product/detail?id={id}";
Map<String, Object> paramMap = new HashMap<String, Object>();
paramMap.put("id", "123");
ResponseEntity<String> response1 = template.getForEntity(url, String.class, paramMap);
System.out.println("result:" + response1.getBody());

exchange

RestTemplate template = new RestTemplate();
String url = "http://127.0.0.1:8800/product/productDetail";
HttpHeaders headers = new HttpHeaders();
headers.set("token", "asdf");
HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<MultiValueMap<String, Object>>(null, headers);
ResponseEntity<String> response = template.exchange(url, HttpMethod.GET, httpEntity, String.class,paramMap);
System.out.println("result:" + response.getBody());

到此這篇關(guān)于SpringBoot使用RestTemplate的文章就介紹到這了,更多相關(guān)SpringBoot使用RestTemplate內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 淺談java是如何做資源回收補救的

    淺談java是如何做資源回收補救的

    這篇文章主要介紹了淺談java是如何做資源回收補救的,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-06-06
  • JAVA 8 ''::'' 關(guān)鍵字詳解

    JAVA 8 ''::'' 關(guān)鍵字詳解

    這篇文章主要介紹了JAVA 8 '::' 關(guān)鍵字,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-09-09
  • Spring Bean的線程安全問題

    Spring Bean的線程安全問題

    Spring容器中的Bean是否線程安全,本文主要介紹了Spring Bean的線程安全問題,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • Fluent Mybatis快速入門詳細教程

    Fluent Mybatis快速入門詳細教程

    由于FluentMybatis是基于mybatis上做封裝和擴展的,所以這里主要聊聊mybatis處理的方式,以及給出FluentMybatis的解放方案。對Fluent Mybatis入門相關(guān)知識感興趣的朋友一起看看吧
    2021-08-08
  • Java無界阻塞隊列DelayQueue詳細解析

    Java無界阻塞隊列DelayQueue詳細解析

    這篇文章主要介紹了Java無界阻塞隊列DelayQueue詳細解析,DelayQueue是一個支持時延獲取元素的無界阻塞隊列,隊列使用PriorityQueue來實現(xiàn),隊列中的元素必須實現(xiàn)Delayed接口,在創(chuàng)建元素時可以指定多久才能從隊列中獲取當前元素,需要的朋友可以參考下
    2023-12-12
  • java 排序算法之歸并排序

    java 排序算法之歸并排序

    本文主要講解了排序算法中的歸并排序,文中運用大量的圖片和代碼講解的非常詳細,感興趣的朋友可以學習一下這篇文章,相信可以幫助到你
    2021-09-09
  • java開發(fā)之Jdbc分頁源碼詳解

    java開發(fā)之Jdbc分頁源碼詳解

    這篇文章主要介紹了java開發(fā)之Jdb分頁源碼詳解,需要的朋友可以參考下
    2020-02-02
  • SpringCloud Zuul基本使用方法匯總

    SpringCloud Zuul基本使用方法匯總

    這篇文章主要介紹了SpringCloud Zuul基本使用方法匯總,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-09-09
  • java多線程之線程安全的單例模式

    java多線程之線程安全的單例模式

    這篇文章主要為大家詳細介紹了java多線程之線程安全的單例模式,文章內(nèi)容全面,感興趣的小伙伴們可以參考一下
    2016-03-03
  • 詳解Spring Boot最新版優(yōu)雅停機的方法

    詳解Spring Boot最新版優(yōu)雅停機的方法

    這篇文章主要介紹了Spring Boot最新版優(yōu)雅停機的相關(guān)知識,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-10-10

最新評論