使用 Spring RestTemplate 调用 rest 服务时自定义请求头(custom HTTP headers)
在 Spring 3.0 中可以通过 HttpEntity 对象自定义请求头信息,如:
private static final String APPLICATION_PDF = "application/pdf"; RestTemplate restTemplate = new RestTemplate(); @Test
public void acceptHeaderUsingHttpEntity() throws Exception {
HttpHeaders headers = new HttpHeaders();
headers.setAccept(singletonList(MediaType.valueOf(APPLICATION_PDF))); ResponseEntity<byte[]> response = restTemplate.exchange("http://example.com/file/123",
GET,
new HttpEntity<byte[]>(headers),
byte[].class); String responseText = PdfTextExtractor.getTextFromPage(new PdfReader(response.getBody()), 1);
assertEquals("Some text in PDF file", responseText);
}
在 Spring 3.1 中有了一个更强大的替代接口 ClientHttpRequestInterceptor,这个接口只有一个方法: intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution),下面是一个例子:
private static final String APPLICATION_PDF = "application/pdf"; RestTemplate restTemplate = new RestTemplate(); @Test
public void acceptHeaderUsingHttpRequestInterceptors() throws Exception {
ClientHttpRequestInterceptor acceptHeaderPdf = new AcceptHeaderHttpRequestInterceptor(
APPLICATION_PDF); restTemplate.setInterceptors(singletonList(acceptHeaderPdf)); byte[] response = restTemplate.getForObject("http://example.com/file/123", byte[].class); String responseText = PdfTextExtractor.getTextFromPage(new PdfReader(response), 1);
assertEquals("Some text in PDF file", responseText);
} class AcceptHeaderHttpRequestInterceptor implements ClientHttpRequestInterceptor {
private final String headerValue; public AcceptHeaderHttpRequestInterceptor(String headerValue) {
this.headerValue = headerValue;
} @Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body,
ClientHttpRequestExecution execution) throws IOException { HttpRequestWrapper requestWrapper = new HttpRequestWrapper(request);
requestWrapper.getHeaders().setAccept(singletonList(MediaType.valueOf(headerValue))); return execution.execute(requestWrapper, body);
}
}
原文:http://svenfila.wordpress.com/2012/01/05/resttemplate-with-custom-http-headers/
使用 Spring RestTemplate 调用 rest 服务时自定义请求头(custom HTTP headers)的更多相关文章
- 通过 Spring RestTemplate 调用带请求体的 Delete 方法(Delete With Request Body)
Spring 框架的RestTemplate 类定义了一些我们在通过 java 代码调用 Rest 服务时经常需要用到的方法,使得我们通过 java 调用 rest 服务时更加方便.简单.但是 Res ...
- 实现在GET请求下调用WCF服务时传递对象(复合类型)参数
WCF实现RESETFUL架构很容易,说白了,就是使WCF能够响应HTTP请求并返回所需的资源,如果有人不知道如何实现WCF支持HTTP请求的,可参见我之前的文章<实现jquery.ajax及原 ...
- RestTemplate 调用本地服务 connection refused
当需要使用服务间的互相调用的时候,通常来说最优雅的方式莫过于Feign调用了.但是有时候特殊原因还是需要使用httpClient之类的工具. 本次我在使用RestTemplate调用本地服务的时候,会 ...
- Spring RestTemplate中几种常见的请求方式GET请求 POST请求 PUT请求 DELETE请求
Spring RestTemplate中几种常见的请求方式 原文地址: https://blog.csdn.net/u012702547/article/details/77917939 版权声明 ...
- Spring Cloud Zuul API服务网关之请求路由
目录 一.Zuul 介绍 二.构建Spring Cloud Zuul网关 构建网关 请求路由 请求过滤 三.路由详解 一.Zuul 介绍 通过前几篇文章的介绍,我们了解了Spring Cloud ...
- Prism for WPF 搭建一个简单的模块化开发框架(四)异步调用WCF服务、WCF消息头添加安全验证Token
原文:Prism for WPF 搭建一个简单的模块化开发框架(四)异步调用WCF服务.WCF消息头添加安全验证Token 为什么选择wcf? 因为好像wcf和wpf就是哥俩,,, 为什么选择异步 ...
- ASP.NET Core - 实现Http自定义请求头策略
前言 在正常的情况下,当我们系统用到JWT认证方式时,需要在Http请求头添加Authorization: XXX,这样在后台服务的控制器中打上[Authorize]授权标签,就限定所有的请求必须通过 ...
- Python3 自定义请求头消息headers
Python3 自定义请求头消息headers 使用python爬虫爬取数据的时候,经常会遇到一些网站的反爬虫措施,一般就是针对于headers中的User-Agent,如果没有对headers进行设 ...
- Ajax设置自定义请求头的两种方法
用自定义请求头token为例 方法一 $.ajax({ type: "post", url:"http://127.0.0.1:4564/bsky-app/templat ...
随机推荐
- cocos2dx进阶学习之瓦片地图编辑器
之前学习了瓦片地图类,现在我们来学习下瓦片地图制作工具 这个是开源的工具,可以从网上下载,下面我们演示下怎么做地图 步骤1 将需要用到的图片放到一个目录下,比如我机器上就是d:\tiled,这些图片是 ...
- Boost.Asio基础(三)
Socket控制 以下的函数进行处理一些高级的socket选项: get_io_service():返回io_service实例 get_option(option):返回socket option对 ...
- js获取控件位置
//获取坐标位置 function getpos(e) { var t=e.offsetTop; var l=e.offsetLeft; var height=e.offsetHeight; whil ...
- 记录一次SQL查询语句
以前发现比较经典的句子,都是记录在电脑上,我今天想搬到博客上,在我看来,写博客真的是一件非常头疼的事,它是内心的一道坎,我必须得跨过它. CREATE TABLE t_jeff ( id int NO ...
- json接口相关(建议结合JFinal框架)
/** * */ package net.wicp.wvqusrtg; import java.util.HashMap; import net.sf.json.JSONArray; import n ...
- java.lang.NoClassDefFoundError: org.ksoap2.transport.HttpTransportSE异常处理
原因就是没有打包进去 因为引用进去 编译时没出出现问题 解决如下
- js中的总结汇总(以后的都收集到这篇)
点1:js中的比较字符串是否相等,js中是用"=="这个来判断是否相等,这点跟java中不一样,java中是.equals()这种方法. 在之前写的ajax的demo中,因为用了. ...
- (Problem 5)Smallest multiple
2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any rema ...
- ognl--数据运转的催化剂
原文链接:http://struts2.group.iteye.com/group/wiki/1353-ognl-catalyst-for-data-operation-in-struts2 首先让我 ...
- Flask web开发 处理Ajax请求
本文介绍如何处理ajax请求, 一.处理ajax的post请求 举例一: js代码举例如下: var id = obj.parentNode.parentNode.id; $.post("/ ...