在 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)的更多相关文章

  1. 通过 Spring RestTemplate 调用带请求体的 Delete 方法(Delete With Request Body)

    Spring 框架的RestTemplate 类定义了一些我们在通过 java 代码调用 Rest 服务时经常需要用到的方法,使得我们通过 java 调用 rest 服务时更加方便.简单.但是 Res ...

  2. 实现在GET请求下调用WCF服务时传递对象(复合类型)参数

    WCF实现RESETFUL架构很容易,说白了,就是使WCF能够响应HTTP请求并返回所需的资源,如果有人不知道如何实现WCF支持HTTP请求的,可参见我之前的文章<实现jquery.ajax及原 ...

  3. RestTemplate 调用本地服务 connection refused

    当需要使用服务间的互相调用的时候,通常来说最优雅的方式莫过于Feign调用了.但是有时候特殊原因还是需要使用httpClient之类的工具. 本次我在使用RestTemplate调用本地服务的时候,会 ...

  4. Spring RestTemplate中几种常见的请求方式GET请求 POST请求 PUT请求 DELETE请求

    Spring RestTemplate中几种常见的请求方式 原文地址: https://blog.csdn.net/u012702547/article/details/77917939   版权声明 ...

  5. Spring Cloud Zuul API服务网关之请求路由

    目录 一.Zuul 介绍 二.构建Spring Cloud Zuul网关 构建网关 请求路由 请求过滤 三.路由详解 一.Zuul 介绍 ​ 通过前几篇文章的介绍,我们了解了Spring Cloud ...

  6. Prism for WPF 搭建一个简单的模块化开发框架(四)异步调用WCF服务、WCF消息头添加安全验证Token

    原文:Prism for WPF 搭建一个简单的模块化开发框架(四)异步调用WCF服务.WCF消息头添加安全验证Token 为什么选择wcf?   因为好像wcf和wpf就是哥俩,,, 为什么选择异步 ...

  7. ASP.NET Core - 实现Http自定义请求头策略

    前言 在正常的情况下,当我们系统用到JWT认证方式时,需要在Http请求头添加Authorization: XXX,这样在后台服务的控制器中打上[Authorize]授权标签,就限定所有的请求必须通过 ...

  8. Python3 自定义请求头消息headers

    Python3 自定义请求头消息headers 使用python爬虫爬取数据的时候,经常会遇到一些网站的反爬虫措施,一般就是针对于headers中的User-Agent,如果没有对headers进行设 ...

  9. Ajax设置自定义请求头的两种方法

    用自定义请求头token为例 方法一 $.ajax({ type: "post", url:"http://127.0.0.1:4564/bsky-app/templat ...

随机推荐

  1. Phonegap-----Media

    Everything in the code: <!DOCTYPE html> <html> <head> <title>Media Example&l ...

  2. Codeforces Round #250 (Div. 2)—A. The Child and Homework

         好题啊,被HACK了.曾经做题都是人数越来越多.这次比赛 PASS人数 从2000直掉 1000人  被HACK  1000多人! ! ! ! 没见过的科技啊 1 2 4 8 这组数 被黑的 ...

  3. 在UITouch事件中画圆圈-iOS8 Swift基础教程

    这篇教程主要内容展示如何利用Core Graphics Framework画圆圈,当用户点击屏幕时随机生成不同大小的圆,这篇教程在Xcode6和iOS8下编译通过. 打开Xcode,新建项目选择Sin ...

  4. Performance Tuning guide 翻译 || Performance Tuning Guide 11G中新增特性

    CSDN 对格式支持比較弱.能够到http://user.qzone.qq.com/88285879/blog/1399382878 看一致的内容. Performance Tuning Guide  ...

  5. ExtJS4.2 - 从 Hello World 到 自定义组件 -01 (为爱女伊兰奋斗)

    ExtJS4.2 - 从 Hello World 到 自定义组件 - 01 经验.概述.项目搭建.国际化.HelloWorld.布局 —— 为爱女伊兰而奋斗 ——少走弯路,简单才是王道 1. 写在前面 ...

  6. Linq实现t-Sql的各种连接

    在ORM框架大行其道的今天,对于.net行业的人,想要学好EF,那Linq的学习在势在必行啊.今天总结下平时比较常用的表连接的用法. Inner Join Linq: var list = (from ...

  7. dhtmlx之dhtmlXGrid显示数据 --大数据

    引用 <link href="../../dhtmlXGridScripts/dhtmlxgrid.css" rel="stylesheet" type= ...

  8. 畅通工程续(Dijkstra算法)

    对Dijkstra算法不是很熟悉,写一下思路,希望通过写博客加深理解 Description 某省自从实行了很多年的畅通工程计划后,终于修建了很多路.不过路多了也不好,每次要从一个城镇到另一个城镇时, ...

  9. Codeforces Round #315 (Div. 2A) 569A Music (模拟)

    题目:Click here 题意:(据说这个题的题意坑了不少人啊~~~)题目一共给了3个数---- T 表示歌曲的长度(s).S 表示下载了歌曲的S后开始第一次播放(也就是说S秒的歌曲是事先下载好的) ...

  10. 【Linux命令】命令行查找文件并进行操作

    查找: #找./下的所有txt文件,输出个数 find ./ -name "*.txt" | wc -l #查找并删除 find ./ -name "*.txt" ...