拦截RestTemplate的请求
RestTemplate一般用于方法内部请求调用,请求报错时难以调试,所以可以为RestTemplate加拦截器进行调试,具体操作如下:
拦截器LoggingClientHttpRequestInterceptor:
package cn.ucmed.otaka.healthcare.interceptor; import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse; import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException; @Slf4j
public class LoggingClientHttpRequestInterceptor implements ClientHttpRequestInterceptor {
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
tranceRequest(request, body);
ClientHttpResponse response = execution.execute(request, body);
traceResponse(response);
return response;
} private void tranceRequest(HttpRequest request, byte[] body) throws UnsupportedEncodingException {
log.info("======= request begin ========"); log.info("uri : {}", request.getURI());
log.info("method : {}", request.getMethod());
log.info("headers : {}", request.getHeaders());
log.info("request body : {}", new String(body, "UTF-8")); log.info("======= request end ========");
} private void traceResponse(ClientHttpResponse response) throws IOException {
StringBuilder inputStringBuilder = new StringBuilder();
try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(response.getBody(), "UTF-8"))) {
String line = bufferedReader.readLine();
while (line != null) {
inputStringBuilder.append(line);
inputStringBuilder.append('\n');
line = bufferedReader.readLine();
}
}
log.info("============================response begin==========================================");
log.info("Status code : {}", response.getStatusCode());
log.info("Status text : {}", response.getStatusText());
log.info("Headers : {}", response.getHeaders());
log.info("Response body: {}", inputStringBuilder.toString());
log.info("=======================response end=================================================");
}
}
在启动类里面对RestTemplate实例添加拦截器
@Bean
public RestTemplate getRestTemplate() {
RestTemplate restTemplate = new RestTemplate();
restTemplate.getInterceptors().add(new LoggingClientHttpRequestInterceptor());
return restTemplate;
}
效果如下:

拦截RestTemplate的请求的更多相关文章
- Struts 2.3.24源码解析+Struts2拦截参数,处理请求,返回到前台过程详析
Struts2官网:http://struts.apache.org/ 目前最新版本:Struts 2.3.24 Struts1已经完全被淘汰了,而Struts2是借鉴了webwork的设计理念而设计 ...
- Java过滤器处理Ajax请求,Java拦截器处理Ajax请求,拦截器Ajax请求
Java过滤器处理Ajax请求,Java拦截器处理Ajax请求,拦截器Ajax请求 >>>>>>>>>>>>>>&g ...
- RestTemplate发送请求并携带header信息 RestTemplate post json格式带header信息
原文地址: http://www.cnblogs.com/hujunzheng/p/6018505.html RestTemplate发送请求并携带header信息 v1.使用restTempl ...
- js拦截全局ajax请求
你是否有过下面的需求:需要给所有ajax请求添加统一签名.需要统计某个接口被请求的次数.需要限制http请求的方法必须为get或post.需要分析别人网络协议等等,那么如何做?想想,如果能够拦截所有a ...
- js 拦截全局 ajax 请求
你是否有过下面的需求:需要给所有ajax请求添加统一签名.需要统计某个接口被请求的次数.需要限制http请求的方法必须为get或post.需要分析别人网络协议等等,那么如何做?想想,如果能够拦截所有a ...
- spring mvc 通过拦截器记录请求数据和响应数据
spring mvc 能过拦截器记录请求数据记录有很多种方式,主要有以下三种: 1:过滤器 2:HandlerInterceptor拦截器 3:Aspect接口控制器 但是就我个人所知要记录返回的数据 ...
- 已拦截跨源请求:同源策略禁止读取位于XXX的远程资源。(原因:CORS 头缺少 'Access-Control-Allow-Origin'
vue+springboot项目 前端发送请求微信 URL:http:/.........(企业微信的路径) 请求成功,数据发送过去可以接收到,处理完毕后发送返回值给我 我这边前端网络响应处可以看到返 ...
- 使用BP拦截POST型请求包 (9.20 第九天)
使用BP拦截POST型请求包 1.安装phpstudy并下载wordpress 文件,安装在phpstudy的www目录下 phpstudy下载地址:https://www.xp.cn/downloa ...
- 通过phpstudy安装wordpress、使用BP拦截POST型请求包
使用BP拦截POST型请求包 一.安装phpstudy https://www.xp.cn/download.html 二.安装wordpress Step1:下载wordpress源码,https: ...
随机推荐
- ASP.NET 异步编程之Async await
本文重点介绍的是.NET Framework4.5 推出的异步编程方案 async await 请先看个5分钟的微软演示的视频:视频地址: https://channel9.msdn.com/Blo ...
- WC2018 文艺汇演《退役的你》
视频网址:https://www.bilibili.com/video/av19333297 谨以此歌献给那些曾与我们并肩前行的退役 OIer 填词 & 视频:Menci 演唱:wxh0109 ...
- vue--设置cookie
Vue-CLI项目-vue-cookie与vue-cookies处理cookie vue-cookie 一.模块的安装 npm install vue-cookie --save #--save可以不 ...
- webpack 入门和常用插件的使用
常用配置参数 module.exports = { context: path.resolve(__dirname, '../'), entry: { app: './src/main.js' }, ...
- Beego 学习笔记9:Boostrap使用介绍
BootStrap布局 1> 下载地址: http://v3.bootcss.com/getting-started/#download 根据自己的需要,下载不同的版本.我这里使用的是1 ...
- go build -tags 的使用
go build 使用tag来实现编译不同的文件 go-tooling-workshop 中关于go build的讲解可以了解到go bulid的一些用法,这篇文章最后要求实现一个根据go bulid ...
- 使用 application.properties 中配置的属性,举例:@Value("${server.port}")
使用 application.properties 中配置的属性:@Value 注解. @RestController public class HelloWorldController { @Val ...
- reflow和repaint理解总结
repaint就是重绘,reflow就是回流 严重性: 在性能优先的前提下,reflow的性能消耗要比repaint的大. 体现: repaint是某个dom元素进行重绘,reflow是整个页面进行重 ...
- python 二、八、十六进制之间的快速转换
一.进制转换 1.2 十进制转二进制 bin(18)--> '0b10010' 去掉0b就是10010 即为十进制18转二进制是10010 十进制转八进制oct(18) --> ...
- The Difference between Gamification and Game-Based Learning
http://inservice.ascd.org/the-difference-between-gamification-and-game-based-learning/ Have you trie ...