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)文章希望大家以后多多支持腳本之家!
- SpringBoot使用RestTemplate實現(xiàn)HTTP請求詳解
- SpringBoot中RestTemplate的使用詳解
- springboot中的RestTemplate使用詳解
- Springboot使用RestTemplate調(diào)用第三方接口的操作代碼
- Springboot之restTemplate的配置及使用方式
- SpringBoot 如何使用RestTemplate發(fā)送Post請求
- 關(guān)于springboot 中使用httpclient或RestTemplate做MultipartFile文件跨服務傳輸?shù)膯栴}
- Springboot RestTemplate 簡單使用解析
- SpringBoot3 RestTemplate配置與使用詳解