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

SpringBoot使用RestTemplate的示例詳解

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

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

POST請(qǐng)求

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方法中設(shè)置header的屬性,當(dāng)需要指定header的屬性值的時(shí)候,使用postForEntity方法。

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

GET請(qǐng)求

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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 淺談java是如何做資源回收補(bǔ)救的

    淺談java是如何做資源回收補(bǔ)救的

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

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

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

    Spring Bean的線程安全問題

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

    Fluent Mybatis快速入門詳細(xì)教程

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

    Java無(wú)界阻塞隊(duì)列DelayQueue詳細(xì)解析

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

    java 排序算法之歸并排序

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

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

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

    SpringCloud Zuul基本使用方法匯總

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

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

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

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

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

最新評(píng)論