前面一篇介绍了如何使用RestTemplate
发起post和get请求,然而也只能知足一些基本的场景,对于一些特殊的如须要设置请求头,添加认证信息等场景,却没有说起能够怎么作,这一篇则至关于进阶版,将主要介绍java
exchange
方法的使用姿式首先一个问题就是为何要设置请求头?git
咱们经过浏览器正常访问的接口,可能经过代码直接访问时,就会提示403github
而这样的缘由,较多的一个可能就是后端的请求作了限制,好比根据请求的agent,判断是否为爬虫;根据referer判断是否要返回数据等等;然后端进行校验的条件中,每每会拿请求头的数据,所以这也就要求咱们在使用时,主动的塞入一些请求头信息spring
直接看RestTemplate
提供的几个Get请求接口,并无发现有设置请求头的地方,是否是就代表无法设置请求头了?json
答案档案是能设置了,具体的使用思路有点相似mvc中的拦截器,自定义一个拦截器,而后在你实际发起请求时,拦截并设置request的请求头后端
注意到 RestTemplate
的父类InterceptingHttpAccessor
提供了一个接收Interceptor的接口org.springframework.http.client.support.InterceptingHttpAccessor#setInterceptors
,这个就是咱们所须要的关键点(讲道理,除非事先就知道有这么个玩意,否则真让你去找,还不必定能找到)浏览器
因此第一步就是写一个ClientHttpRequestInterceptor
类,添加请求头安全
public class UserAgentInterceptor implements ClientHttpRequestInterceptor { @Override public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException { HttpHeaders headers = request.getHeaders(); headers.add(HttpHeaders.USER_AGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36"); return execution.execute(request, body); } }
下一步就是在建立RestTemplate对象以后,声明解释器并测试使用了网络
@Test public void testGetHeader() { String url = "http://localhost:8080/agent?name=一灰灰Blog"; RestTemplate restTemplate = new RestTemplate(); restTemplate.setInterceptors(Collections.singletonList(new UserAgentInterceptor())); ResponseEntity<String> response = restTemplate.getForEntity(url, String.class); System.out.println(response.getStatusCode() + " | " + response.getBody()); }
首先在测试以前,先搭一个服务,简单判断agent,不知足条件的直接403, 后端mock代码以下mvc
@ResponseBody @RequestMapping(path = "agent") public String agent(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "name", required = false) String name) throws IOException { String agent = request.getHeader(HttpHeaders.USER_AGENT); if (StringUtils.isEmpty(agent) || !agent.contains("WebKit")) { response.sendError(403, " illegal agent "); } return "welcome " + name; }
上面执行后输出以下,添加请求头后正常返回
固然也须要对比下不设置agent的状况了,直接抛了一个异常出来了(说明,不显示覆盖User-Agent时,后端接收到的agent为: Java/1.8.0_171
上面虽然只给了设置User-Agent的例子,可是其余的请求头,都是能够放在自定义的Interceptor
中添加进去的
固然get请求使用的这种姿式,对于post而言或者对于其余的http请求方法而言,都是通用的,而对于post请求来讲,还有另一种方式,就是requset参数,能够携带request headers
首先mock一个后端接口
@ResponseBody @RequestMapping(path = "post", method = {RequestMethod.GET, RequestMethod.OPTIONS, RequestMethod.POST}, produces = "charset/utf8") public String post(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "email", required = false) String email, @RequestParam(value = "nick", required = false) String nick) throws IOException { String agent = request.getHeader(HttpHeaders.USER_AGENT); if (StringUtils.isEmpty(agent) || !agent.contains("WebKit")) { response.sendError(403, " illegal agent "); return null; } return "success email=" + email + "&nick=" + URLEncoder.encode(nick, "UTF-8") + "&status=success"; }
简单的使用姿式以下
@Test public void testPostHeader() { String url = "http://localhost:8080/post"; String email = "test@hhui.top"; String nick = "一灰灰Blog"; MultiValueMap<String, String> params = new LinkedMultiValueMap<>(); params.add("email", email); params.add("nick", nick); HttpHeaders headers = new HttpHeaders(); headers.add(HttpHeaders.USER_AGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) " + "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36"); HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(params, headers); RestTemplate restTemplate = new RestTemplate(); ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class); System.out.println(response.getStatusCode() + " | " + response.getBody()); }
从上面代码能够看出,具体的使用姿式相比于不添加请求头时,只是多了一个封装
HttpHeaders
对象中MultiValueMap
中HttpEntity
对象,将这个做为post的请求request参数传入固然做为对比,当不加入headers时,看下返回什么鬼, 406异常,可是咱们后端定义的是403,为何会返回406呢?
另外还会关注到RestTemplate还提供了一个exchange方法,这个至关于一个公共的请求模板,使用姿式和get/post没有什么区别,只是能够由调用发本身来选择具体的请求方法
使用exchange对上面的post请求进行简单的替换以下, 基本上除了多一个参数以外没有什么区别了
@Test public void testPostHeader() { String url = "http://localhost:8080/post"; String email = "test@hhui.top"; String nick = "一灰灰Blog"; MultiValueMap<String, String> params = new LinkedMultiValueMap<>(); params.add("email", email); params.add("nick", nick); HttpHeaders headers = new HttpHeaders(); headers.add(HttpHeaders.USER_AGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) " + "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36"); HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(params, headers); RestTemplate restTemplate = new RestTemplate(); ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, request, String.class); System.out.println(response.getStatusCode() + " | " + response.getBody()); }
那么问题来了,为何要有这个东西?或者说这个接口的提供能够带来什么好处?
前面的post参数提交,其实默认采用的是 application/x-www-form-urlencoded
方式,便是咱们最多见的表单提交方式,在浏览器中的表现形式以下
此外,还有一种直接提交json串的方式,在前文 《180730-Spring之RequestBody的使用姿式小结》中有说明,具体浏览器中表现形式为
因此接下来的问题就是,RestTemplate要怎么处理呢?
建议在看下面的内容以前,先看一下上面的那篇博文,理解下RequestBody
是什么东西
首先搭建一个后端
@Data @NoArgsConstructor @AllArgsConstructor public static class Req { private String key; private Integer size; } @ResponseBody @RequestMapping(value = "/body", method = {RequestMethod.POST, RequestMethod.GET, RequestMethod.OPTIONS}) public String body(@RequestBody Req req) { return req.toString(); }
而后使用方式,无非就是在请求头中添加下Content-Type为Application/json
@Test public void testPostRequestBody() { String url = "http://localhost:8080/body"; String email = "test@hhui.top"; String nick = "一灰灰Blog"; Map<String, String> params = new HashMap<>(); params.put("email", email); params.put("nick", nick); HttpHeaders headers = new HttpHeaders(); headers.add(HttpHeaders.CONTENT_TYPE, "application/json"); HttpEntity<Map<String, String>> request = new HttpEntity<>(params, headers); RestTemplate restTemplate = new RestTemplate(); ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class); System.out.println(response.getStatusCode() + " | " + response.getBody()); }
注意下post参数,是放在Map容器中,而不是以前的MultiValueMap
运行时截图以下
post除了传表单数据(json串)以外,还有一个常见的就是上传文件了,实际上使用RestTemplate来实现文件上传,算是比较简单的了,和前面的使用基本上也没有什么差异,只是将文件做为params参数而已
首先搭建一个Controller后端服务,简单的获取文件内容,并返回
@ResponseBody @PostMapping(path = "file") public String file(MultipartHttpServletRequest request) throws IOException { MultipartFile file = request.getFile("file"); if (file == null) { return "no file!"; } BufferedReader reader = new BufferedReader(new InputStreamReader(file.getInputStream())); StringBuilder builder = new StringBuilder(); String line = reader.readLine(); while (line != null) { builder.append(line); line = reader.readLine(); } reader.close(); return builder.toString(); }
而后就是实际的测试用例,将文件包装在FileSystemResource
对象中,而后塞入MultiValueMap
中,注意下面的使用并无显示添加请求头,而这种时候,content-type 经过断点查看实际为 content-type = multipart/form-data;
@Test public void testPostFile() { String url = "http://localhost:8080/file"; FileSystemResource resource = new FileSystemResource(new File("/tmp/test.txt")); MultiValueMap<String, Object> params = new LinkedMultiValueMap<>(); params.add("file", resource); params.add("fileName", "test.txt"); RestTemplate restTemplate = new RestTemplate(); ResponseEntity<String> response = restTemplate.postForEntity(url, params, String.class); System.out.println(response); }
本篇主要介绍如何给RestTemplate发起的请求,添加请求头,以及完成某些特定的请求,下面小结一下使用姿式
两种方式
Interceptors
,在拦截器中主动添加上对应的请求头便可,适用于为全部的请求添加统一的请求头的场景
postForXXX
和 exchange
两种请求方式而言,一样本身设置请求头HttpHeader
,而后将请求头和params封装到HttpEntity
,做为request参数提交便可json串的提交
Applicaiton/json
HttpEntity
中,发起请求便可文件上传
MultiValueMap
中便可,和普通的请求方式没有什么区别初级篇介绍了如何使用RestTemplate发起简单的GET/POST请求;
中级篇则介绍请求的过程当中添加设置请求头,以及某些特殊的请求能够怎么处理
显然还会有高级篇,除了上面的东西,咱们还须要知道些什么呢?
关于高级篇,坐等更新
一灰灰的我的博客,记录全部学习和工做中的博文,欢迎你们前去逛逛
尽信书则不如,已上内容,纯属一家之言,因我的能力有限,不免有疏漏和错误之处,如发现bug或者有更好的建议,欢迎批评指正,不吝感激
一灰灰blog
知识星球