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)文章希望大家以后多多支持腳本之家!
- SpringBoot使用RestTemplate實(shí)現(xiàn)HTTP請(qǐng)求詳解
- SpringBoot中RestTemplate的使用詳解
- springboot中的RestTemplate使用詳解
- Springboot使用RestTemplate調(diào)用第三方接口的操作代碼
- Springboot之restTemplate的配置及使用方式
- SpringBoot 如何使用RestTemplate發(fā)送Post請(qǐng)求
- 關(guān)于springboot 中使用httpclient或RestTemplate做MultipartFile文件跨服務(wù)傳輸?shù)膯栴}
- Springboot RestTemplate 簡(jiǎn)單使用解析
- SpringBoot3 RestTemplate配置與使用詳解
相關(guān)文章
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詳解Spring Boot最新版優(yōu)雅停機(jī)的方法
這篇文章主要介紹了Spring Boot最新版優(yōu)雅停機(jī)的相關(guān)知識(shí),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10