当前位置:网站首页>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=================================================");
}
}
边栏推荐
- Yyds dry goods inventory leetcode question set 911 - 920
- Graphical data model for system design
- 全局锁、表锁、行锁
- JDBC quick start tutorial
- 文章名字
- [csp]202012-2 optimal threshold for period end forecast
- String的split方法的使用
- grpc-swift入门
- Schedule update | 2022 Microsoft and Intel hacker song competition is in hot registration
- Figma从入门到放弃
猜你喜欢
随机推荐
Tutoriel de démarrage rapide JDBC
(5) Outputs and outputs
Application case of smart micro 32-bit MCU for server application cooling control
qemu+gdb小节
Idea common shortcut keys
Saturated! Can't future programmers work anymore?
Memory control of node
Cesium抛物线方程
118. Yanghui triangle (dynamic planning)
JDBC快速入门教程
How to change Golan back to the English version when it becomes the Chinese version
(6) Control statement if/else switch
Evolution and thinking of Taobao native R & D mode | DX R & D mode
Exclusive interview with oppo find X5 Product Manager: deeply cultivate self-developed chips to create the ultimate flagship experience with the highest standards
TensorFlow求梯度时提示TypeError: unsupported operand type(s) for *: ‘float‘ and ‘NoneType‘
MIPS 通用寄存器 + 指令
Tensorflow prompts typeerror: unsupported operand type (s) for *: 'float' and 'nonetype‘
vant3+ts 封装uploader上传图片组件
32-bit MCU mm32f0040 with smart micro high performance M0 kernel
Office application cannot start normally 0xc0000142






![[CSP]202012-2期末预测之最佳阈值](/img/40/9b59bd692bcfe05d16614cc6d55d1f.png)
