SpringBoot使用RestTemplate的示例详解
RestTemplate 是由 Spring 提供的一个 HTTP 请求工具,它提供了常见的REST请求方案的模版,例如 GET 请求、POST 请求、PUT 请求、DELETE 请求以及一些通用的请求执行方法 exchange 以及 execute。RestTemplate 继承自 InterceptingHttpAccessor 并且实现了 RestOperations 接口,其中 RestOperations 接口定义了基本的 RESTful 操作,这些操作在 RestTemplate 中都得到了实现。
POST请求
postForObject
1、使用LinkedMultiValueMap作为参数(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作为参数(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作为参数(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作为参数(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作为参数(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为参数(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方法的区别主要在于可以在postForEntity方法中设置header的属性,当需要指定header的属性值的时候,使用postForEntity方法。
exchange方法和postForEntity类似,但是更灵活,exchange还可以调用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());
到此这篇关于SpringBoot使用RestTemplate的文章就介绍到这了,更多相关SpringBoot使用RestTemplate内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
Java中的数组复制(clone与arraycopy)代码详解
这篇文章主要介绍了Java中的数组复制(clone与arraycopy)代码详解,本文并未全部介绍数组复制的几种方式,仅对clone和copy的相关内容进行了解析,具有一定参考价值,需要的朋友可以了解下。2017-11-11Spring Boot中的max-http-header-size配置方式
这篇文章主要介绍了Spring Boot中的max-http-header-size配置方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2022-09-09MybatisPlus EntityWrapper如何自定义SQL
这篇文章主要介绍了MybatisPlus EntityWrapper如何自定义SQL,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2022-03-03Java中BeanUtils.copyProperties的11个坑总结
我们日常开发中,经常涉及到DO、DTO、VO对象属性拷贝赋值,很容易想到org.springframework.beans.BeanUtils的copyProperties,它会自动通过反射机制获取源对象和目标对象的属性,pyProperties,会有好几个坑呢,本文将给大家总结一下遇到的坑,需要的朋友可以参考下2023-05-05
最新评论