zoukankan      html  css  js  c++  java
  • 拦截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('
    ');
                    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;
        }

    效果如下:



  • 相关阅读:
    oracle 分页sql
    Oracle 11g数据库详细安装步骤图解
    轻量级ORM框架——第二篇:Dapper中的一些复杂操作和inner join应该注意的坑
    轻量级ORM框架——第一篇:Dapper快速学习
    MY SQL 两种安装方式
    在本地库不连接远远程库的情况下操作远程库-----sql server
    学习Java泛型(记得通配符)
    学习java泛型(还是集合)
    学习java011902迭代器
    学习Java-继续集合
  • 原文地址:https://www.cnblogs.com/miaoying/p/11132942.html
Copyright © 2011-2022 走看看