当前位置:网站首页>Resttemplateconfig configuration print request response log under soringboot
Resttemplateconfig configuration print request response log under soringboot
2022-06-12 17:48:00 【Tata Tata y】
RestTemplateConfig Configure print log
Original excerpt from here https://blog.csdn.net/laow1314/article/details/120757485
@Configuration
public class RestTemplateConfig {
/** * Initialize connection factory * @return */
@Bean
public ClientHttpRequestFactory simpleClientHttpRequestFactory(){
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
/** Connection timeout */
factory.setConnectTimeout(15000);
/** Read timeout */
factory.setReadTimeout(5000);
return factory;
}
/** * Initialize request template * @param simpleClientHttpRequestFactory * @return */
@Bean
public RestTemplate restTemplate(ClientHttpRequestFactory simpleClientHttpRequestFactory,@Qualifier("loggingRequestInterceptor") LoggingRequestInterceptor loggingRequestInterceptor){
RestTemplate restTemplate = new RestTemplate(new BufferingClientHttpRequestFactory(simpleClientHttpRequestFactory));
List<ClientHttpRequestInterceptor> interceptors = new ArrayList<>();
interceptors.add(loggingRequestInterceptor);
restTemplate.setInterceptors(interceptors);
return restTemplate;
}
}
LoggingRequestInterceptor Interception class
@Component("loggingRequestInterceptor")
public class LoggingRequestInterceptor implements ClientHttpRequestInterceptor {
Logger log = LoggerFactory.getLogger(getClass());
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
traceRequest(request, body);
ClientHttpResponse response = execution.execute(request, body);
traceResponse(response);
return response;
}
private void traceRequest(HttpRequest request, byte[] body) throws IOException {
log.debug("===========================request begin================================================");
log.debug("URI : {}", request.getURI());
log.debug("Method : {}", request.getMethod());
log.debug("Headers : {}", request.getHeaders() );
log.debug("Request body: {}", new String(body, "UTF-8"));
log.debug("==========================request end================================================");
}
private void traceResponse(ClientHttpResponse response) throws IOException {
StringBuilder inputStringBuilder = new StringBuilder();
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.debug("============================response begin==========================================");
log.debug("Status code : {}", response.getStatusCode());
log.debug("Status text : {}", response.getStatusText());
log.debug("Headers : {}", response.getHeaders());
log.debug("Response body: {}", inputStringBuilder.toString());
log.debug("=======================response end=================================================");
}
}
边栏推荐
- 全局锁、表锁、行锁
- New media operation material website sharing enables you to create current affairs with half the effort
- 徽商期货公司开户可靠,交易安全吗?
- WinForm, crystal report making
- Use of split method of string
- office应用程序无法正常启动0xc0000142
- Common dependencies of SSM
- Application case of smart micro 32-bit MCU for server application cooling control
- Risc-v ide mounriver studio v1.60 update point introduction
- 566. 重塑矩阵
猜你喜欢

JDBC several pits

论文《Deep Interest Evolution Network for Click-Through Rate Prediction》

SSM integrates FreeMarker and common syntax

Make good use of IDE, speed up R & D efficiency by 100%

JDBC快速入門教程

Byte flybook Human Resources Kit three sides

Arm64 stack backtracking

TensorFlow求梯度时提示TypeError: unsupported operand type(s) for *: ‘float‘ and ‘NoneType‘

1723. minimum time to complete all work

vant3+ts+pinia tab选项卡列表页面点击进详情,详情页返回tab高亮在原位置,刷新高亮默认在第一项
随机推荐
Some introduction to FPC flexible circuit board design
有关cookie的用法介绍
一种好用、易上手的小程序IDE
Deep interest evolution network for click through rate prediction
認識函數原創
Memory control of node
Are Huishang futures accounts reliable? Is the fund safe?
EASYCODE template
(4) Golang operator
MIPS 通用寄存器 + 指令
字节飞书人力资源套件三面
徽商期货开户可靠吗?资金安全吗?
Authorization in Golang ProjectUseing Casbin
SSM集成FreeMarker以及常用语法
Arm64棧回溯
利用小程序快速生成App,只需七步
Message queuing MySQL tables that store message data
Use of split method of string
TensorFlow2训练数据集的两种方式
用好IDE,研发效能提速100%