SpringBoot RestTemplate打印http请求响应

时间:2024-03-17 20:45:39

前言

SpringBoot 的 RestTemplate是一个比较方便的http请求框架,在调试的时候我们需要查看http的request和response信息RestTemplate不会主动帮我们打印出来,但是我们可以在RestTemplate的拦截器中新增一个我们的log拦截器即可打印Http的必要信息。

代码实现如下:

    private static final Logger logger = LoggerFactory.getLogger(WebMvcConfig.class);

    private ClientHttpRequestInterceptor logClientHttpRequestInterceptor() {
        return ((httpRequest, bytes, clientHttpRequestExecution) -> {
            logger.debug("------------------------rest request----------------------------");
            logger.debug("request url:" + httpRequest.getURI());
            logger.debug("request header:" + httpRequest.getHeaders().toString());
            logger.debug("request method:" + httpRequest.getMethodValue());
            logger.debug("request body:" + new String(bytes));
            logger.debug("----------------------------------------------------------------");

            ClientHttpResponse response = clientHttpRequestExecution.execute(httpRequest, bytes);
            byte[] bys = new byte[0];
            if(response.getStatusCode() == HttpStatus.OK)
                bys = new byte[response.getBody().available()];

            logger.debug("-----------------------rest response----------------------------");
            logger.debug("response code:" + response.getStatusCode());
            logger.debug("response code msg:" + response.getStatusText());
            logger.debug("response headers:" + response.getHeaders().toString());
            logger.debug("response body:" + new String(bys, "utf-8"));
            logger.debug("----------------------------------------------------------------");

            return new ClientHttpResponseWrapper(response, bys);
        });
    }

    class ClientHttpResponseWrapper implements ClientHttpResponse {

        private ClientHttpResponse response;
        private InputStream in;

        public ClientHttpResponseWrapper(ClientHttpResponse response, byte[] body) {
            this.response = response;
            in = new ByteArrayInputStream(body);
        }

        @Override
        public HttpStatus getStatusCode() throws IOException {
            return response.getStatusCode();
        }

        @Override
        public int getRawStatusCode() throws IOException {
            return response.getRawStatusCode();
        }

        @Override
        public String getStatusText() throws IOException {
            return response.getStatusText();
        }

        @Override
        public void close() {
            try {
                in.close();
                response.close();
            } catch (IOException e) { }
        }

        @Override
        public InputStream getBody() throws IOException {
            return in;
        }

        @Override
        public HttpHeaders getHeaders() {
            return response.getHeaders();
        }
    }

    @Bean
    public RestTemplate restTemplate() {
        SimpleClientHttpRequestFactory requestFactory = new         
        SimpleClientHttpRequestFactory();
        requestFactory.setConnectTimeout(1000);
        requestFactory.setReadTimeout(1000);

        RestTemplate restTemplate = new RestTemplate(requestFactory);
               restTemplate.setInterceptors(Lists.newArrayList(logClientHttpRequestInterceptor()));

        return restTemplate;
    }

效果图:

SpringBoot RestTemplate打印http请求响应