Spring RestTemplate用法
2021/6/3 18:22:27
本文主要是介绍Spring RestTemplate用法,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
RestTemplate简介
RestTemplate对HTTP请求进行了封装,进行请求的时候可以保留cookie,在下次请求的时候使用;
postForEntity与postForObject功能类似,可以从源码上面看出postForEntity进行了为空判断;
如果想在GET请求的时候带上cookie,不能使用getForEntity方法,需要使用exchange方法;
注意GET请求参数添加有两种方法:直接添加到URL或者构建MultiValueMap对象;
代码示例
import org.springframework.http.*; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.web.client.RestTemplate; import java.util.List; public class RestRequest { public static void main(String[] args) { List<String> cookies = login(); postForValue(cookies); jsonPost(cookies); getForValue(cookies); getForValueV2(cookies); } // 有些服务器参数需要带在url上面 private static void getForValueV2(List<String> cookies) { RestTemplate restTemplate = new RestTemplate(); String url = "http://ip:port/xxx?key=value"; HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.put(HttpHeaders.COOKIE, cookies); HttpEntity<MultiValueMap<String, String>> httpEntity = new HttpEntity<>(null, httpHeaders); ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.GET, httpEntity, String.class); String body = responseEntity.getBody(); System.out.println(body); } // 带cookie的Get请求; private static void getForValue(List<String> cookies) { RestTemplate restTemplate = new RestTemplate(); String url = "http://ip:port/xxx"; MultiValueMap<String, String> params = new LinkedMultiValueMap<>(); params.add("key", "value"); HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.put(HttpHeaders.COOKIE, cookies); HttpEntity<MultiValueMap<String, String>> httpEntity = new HttpEntity<>(params, httpHeaders); ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.GET, httpEntity, String.class); String body = responseEntity.getBody(); System.out.println(body); } // json提交请求,带登陆cookie private static void jsonPost(List<String> cookies) { RestTemplate restTemplate = new RestTemplate(); String url = "http://ip:port/xxx"; String jsonString = "{}"; // json字符串,可以嵌套多级 HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.put(HttpHeaders.COOKIE, cookies); MediaType mediaType = MediaType.parseMediaType("application/json;charset=UTF-8"); httpHeaders.setContentType(mediaType); httpHeaders.add("Accept", MediaType.APPLICATION_JSON.toString()); HttpEntity<String> httpEntity = new HttpEntity<>(jsonString, httpHeaders); ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, httpEntity, String.class); String body = responseEntity.getBody(); System.out.println(body); } // post提交请求,带登陆cookie private static void postForValue(List<String> cookies) { RestTemplate restTemplate = new RestTemplate(); String url = "http://ip:port/xxx"; MultiValueMap<String, String> params = new LinkedMultiValueMap<>(); params.add("key", "value"); HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.put(HttpHeaders.COOKIE, cookies); HttpEntity<MultiValueMap<String, String>> httpEntity = new HttpEntity<>(params, httpHeaders); ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, httpEntity, String.class); String body = responseEntity.getBody(); System.out.println(body); } // 登陆请求,请求之后把cookie保留下来 private static List<String> login() { RestTemplate restTemplate = new RestTemplate(); String url = "http://ip:port/xxx"; MultiValueMap<String, String> params = new LinkedMultiValueMap<>(); params.add("key", "value"); HttpHeaders httpHeaders = new HttpHeaders(); HttpEntity<MultiValueMap<String, String>> httpEntity = new HttpEntity<>(params, httpHeaders); ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, httpEntity, String.class); HttpHeaders headers = responseEntity.getHeaders(); return headers.get("Set-Cookie"); } }
这篇关于Spring RestTemplate用法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-10-01基于Python+Vue开发的医院门诊预约挂号系统
- 2024-10-01基于Python+Vue开发的旅游景区管理系统
- 2024-10-01RestfulAPI入门指南:打造简单易懂的API接口
- 2024-10-01初学者指南:了解和使用Server Action
- 2024-10-01Server Component入门指南:搭建与配置详解
- 2024-10-01React 中使用 useRequest 实现数据请求
- 2024-10-01使用 golang 将ETH账户的资产平均分散到其他账户
- 2024-10-01JWT用户校验课程:从入门到实践
- 2024-10-01Server Component课程入门指南
- 2024-09-30Dnd-Kit学习:新手快速入门指南