From f4846bdc0767c9d2e715f21bbcfcc20fabef323f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9B=B9=E9=B9=8F=E9=A3=9E?= Date: Wed, 19 Jun 2024 13:35:36 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fix:=20=E6=B7=BB=E5=8A=A0=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E9=94=99=E8=AF=AF=E5=BC=82=E5=B8=B8=E7=9A=84?= =?UTF-8?q?=E5=8F=8B=E5=A5=BD=E6=8F=90=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../base/core/exception/BaseGlobalExceptionHandle.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/nflg_project_dev/nflg-boot-base/nflg-boot-core/src/main/java/com/nflg/product/base/core/exception/BaseGlobalExceptionHandle.java b/nflg_project_dev/nflg-boot-base/nflg-boot-core/src/main/java/com/nflg/product/base/core/exception/BaseGlobalExceptionHandle.java index 114ccd6d..4c9faccc 100644 --- a/nflg_project_dev/nflg-boot-base/nflg-boot-core/src/main/java/com/nflg/product/base/core/exception/BaseGlobalExceptionHandle.java +++ b/nflg_project_dev/nflg-boot-base/nflg-boot-core/src/main/java/com/nflg/product/base/core/exception/BaseGlobalExceptionHandle.java @@ -1,6 +1,7 @@ package com.nflg.product.base.core.exception; import cn.hutool.core.util.StrUtil; +import com.fasterxml.jackson.databind.exc.InvalidFormatException; import lombok.extern.slf4j.Slf4j; import nflg.product.common.constant.STATE; import nflg.product.common.vo.ResultVO; @@ -167,4 +168,12 @@ public class BaseGlobalExceptionHandle { //return ResultVO.error(e.getState(), e.getMessage()); return ResultVO.error(e.getState(), e.getMessage(), e.getData()); } + + @ExceptionHandler(value = InvalidFormatException.class) + @ResponseBody + public ResultVO handleInvalidFormatException(ErrorMsgException e) { + log.error(e.getMessage(), e); + //return ResultVO.error(e.getState(), e.getMessage()); + return ResultVO.error(STATE.ParamErr, "数据格式错误,请修改后再提交: " + e.getMessage()); + } } From d5f948916b6d6378be7d4600da022aa5b168f818 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9B=B9=E9=B9=8F=E9=A3=9E?= Date: Wed, 19 Jun 2024 13:35:58 +0800 Subject: [PATCH 2/2] =?UTF-8?q?optimize:=20=E4=BC=98=E5=8C=96http=E8=AF=B7?= =?UTF-8?q?=E6=B1=82=E5=92=8C=E5=93=8D=E5=BA=94=E8=BE=93=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../advice/LoggingRequestBodyAdvice.java | 41 +++++++++++++++---- .../advice/LoggingResponseBodyAdvice.java | 6 ++- 2 files changed, 39 insertions(+), 8 deletions(-) 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); }