使用 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 ...
随机推荐
- struts文件上传和下载
文件上传 jsp中 <a href="/file/new.action">文件上传案例</a> fileaction中 @Override public S ...
- hdu 4730 We Love MOE Girls
http://acm.hdu.edu.cn/showproblem.php?pid=4730 直接用string类处理字符串. AC代码: #include<iostream> #incl ...
- 【剑指Offer学习】【面试题18 :树的子结构】
题目:输入两棵二叉树A 和B.推断B 是不是A 的子结构. 二叉树结点的定义: /** * 二叉树的树结点 */ public static class BinaryTreeNode { int va ...
- Basic DataList
一,效果图. 二,源代码. <!DOCTYPE html><html><head> <meta charset="UTF-8"> & ...
- 集合如何判断null
转http://blog.csdn.net/baple/article/details/8604585 java判断list为空 分类: JAVA 2013-02-23 08:47 18368人阅读 ...
- Python 2.7 学习笔记 基本知识
python是一种解释型的.面向对象的.带有动态语义的高级程序设计语言.本文介绍下python的基本知识. 一.安装 各种操作系统有自己的安装方法,linux系统一般都自带了python的环境.这里不 ...
- Linux下的在线播放神器
Linux下的在线播放神器:一个是Amarok缺点是,每个音乐源都要更新后才能播放. 在一个就是中国造的:linux deepin下的深度音乐,缺点就是连不上.反正我是连不上
- sequence1(暴力)
sequence1 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total ...
- BZOJ 3196: Tyvj 1730 二逼平衡树( 树套树 )
这道题做法应该很多吧.... 我用了线段树套treap.... -------------------------------------------------------------------- ...
- javascript每日一练(十四)——弹性运动
一.弹性运动 运动原理:加速运动+减速运动+摩擦运动: <!doctype html> <html> <head> <meta charset="u ...