diff --git a/nflg_project_dev/nflg-bom-new/src/main/java/com/nflg/product/bomnew/advice/LoggingRequestBodyAdvice.java b/nflg_project_dev/nflg-bom-new/src/main/java/com/nflg/product/bomnew/advice/LoggingRequestBodyAdvice.java index a414ef07..62254e4a 100644 --- a/nflg_project_dev/nflg-bom-new/src/main/java/com/nflg/product/bomnew/advice/LoggingRequestBodyAdvice.java +++ b/nflg_project_dev/nflg-bom-new/src/main/java/com/nflg/product/bomnew/advice/LoggingRequestBodyAdvice.java @@ -9,12 +9,16 @@ import nflg.product.common.vo.ResultVO; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.core.MethodParameter; +import org.springframework.http.HttpHeaders; import org.springframework.http.HttpInputMessage; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdvice; +import java.io.*; import java.lang.reflect.Type; +import java.nio.charset.StandardCharsets; +import java.util.stream.Collectors; /** * @author 曹鹏飞 @@ -35,13 +39,16 @@ public class LoggingRequestBodyAdvice implements RequestBodyAdvice { @Override public HttpInputMessage beforeBodyRead(HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type, Class> aClass) { - return httpInputMessage; - } - - @Override - public Object afterBodyRead(Object o, HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type, Class> aClass) { try { - log.info("请求参数: {}", JsonUtil.toJson(o)); + String contentType = httpInputMessage.getHeaders().getFirst("Content-Type"); + if (StrUtil.containsIgnoreCase(contentType, "multipart")) { + return httpInputMessage; + } + String bodyContent; + try (BufferedReader reader = new BufferedReader(new InputStreamReader(httpInputMessage.getBody(), StandardCharsets.UTF_8))) { + bodyContent = reader.lines().collect(Collectors.joining()); + log.info("请求参数: {}", bodyContent); + } log.info("请求头: {}", JsonUtil.toJson(httpInputMessage.getHeaders())); String token = httpInputMessage.getHeaders().getFirst("Authorization"); if (StrUtil.isNotBlank(token)) { @@ -50,9 +57,29 @@ public class LoggingRequestBodyAdvice implements RequestBodyAdvice { log.info("请求用户: {}", JsonUtil.toJson(result.getData())); } } + if (StrUtil.isBlank(bodyContent)) { + return httpInputMessage; + } else { + return new HttpInputMessage() { + @Override + public InputStream getBody() throws IOException { + return new ByteArrayInputStream(bodyContent.getBytes()); + } + + @Override + public HttpHeaders getHeaders() { + return httpInputMessage.getHeaders(); + } + }; + } } catch (Exception ex) { - log.error("读取请求体出错", ex); + log.error("读取请求数据出错", ex); + return httpInputMessage; } + } + + @Override + public Object afterBodyRead(Object o, HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type, Class> aClass) { return o; } diff --git a/nflg_project_dev/nflg-bom-new/src/main/java/com/nflg/product/bomnew/advice/LoggingResponseBodyAdvice.java b/nflg_project_dev/nflg-bom-new/src/main/java/com/nflg/product/bomnew/advice/LoggingResponseBodyAdvice.java index 1fc4e76f..6c4de379 100644 --- a/nflg_project_dev/nflg-bom-new/src/main/java/com/nflg/product/bomnew/advice/LoggingResponseBodyAdvice.java +++ b/nflg_project_dev/nflg-bom-new/src/main/java/com/nflg/product/bomnew/advice/LoggingResponseBodyAdvice.java @@ -1,5 +1,6 @@ package com.nflg.product.bomnew.advice; +import cn.hutool.core.util.StrUtil; import com.nflg.product.bomnew.util.JsonUtil; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; @@ -32,7 +33,10 @@ public class LoggingResponseBodyAdvice implements ResponseBodyAdvice { @Override public Object beforeBodyWrite(Object o, MethodParameter methodParameter, MediaType mediaType, Class> aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) { try { - log.info("响应数据: {}", JsonUtil.toJson(o)); + String contentType = serverHttpResponse.getHeaders().getFirst("Content-Type"); + if (!StrUtil.containsIgnoreCase(contentType, "octet-stream")) { + log.info("响应数据: {}", JsonUtil.toJson(o)); + } } catch (Exception ex) { log.error("读取响应数据出错", ex); }