From a1ba34955c7b6e18581814c863a23a307514d841 Mon Sep 17 00:00:00 2001 From: lihongjie0209 Date: Tue, 25 Jul 2023 16:49:04 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=A8=E5=B1=80=E5=BC=82=E5=B8=B8=E5=A4=84?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../coal/config/GlobalExceptionHandler.java | 8 +++- .../coal/config/LogRequestBodyAdvice.java | 38 +++++++++++++++++++ .../lihongjie/coal/dto/CoalBlendRequest.java | 7 ++++ .../lihongjie/coal/service/CoalService.java | 15 ++++++++ 4 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 src/main/java/cn/lihongjie/coal/config/LogRequestBodyAdvice.java diff --git a/src/main/java/cn/lihongjie/coal/config/GlobalExceptionHandler.java b/src/main/java/cn/lihongjie/coal/config/GlobalExceptionHandler.java index cd6af78d..1a58fac6 100644 --- a/src/main/java/cn/lihongjie/coal/config/GlobalExceptionHandler.java +++ b/src/main/java/cn/lihongjie/coal/config/GlobalExceptionHandler.java @@ -6,6 +6,8 @@ import jakarta.servlet.http.HttpServletRequest; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; +import org.springframework.web.context.request.RequestAttributes; +import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.method.HandlerMethod; import java.util.Arrays; @@ -35,15 +37,17 @@ public class GlobalExceptionHandler { } - private void logException(Exception ex, HttpServletRequest request, HandlerMethod handlerMethod) { + private void logException(Throwable ex, HttpServletRequest request, HandlerMethod handlerMethod) { // Arrays.stream(handlerMethod.getMethodParameters()).map(x -> x.getParameterName() + ": ") - log.info("接口调用异常: {}\nURL:{} {}\nMethod: {}\n", + log.info("接口调用异常: {}\nurl:{} {}\nmethod: {}\nrequestBody: {}", ex.getMessage() == null ? "no message" : ex.getMessage(), request.getMethod(), request.getRequestURL(), handlerMethod.toString(), + RequestContextHolder.currentRequestAttributes().getAttribute("__request_body", + RequestAttributes.SCOPE_REQUEST), ex); } } diff --git a/src/main/java/cn/lihongjie/coal/config/LogRequestBodyAdvice.java b/src/main/java/cn/lihongjie/coal/config/LogRequestBodyAdvice.java new file mode 100644 index 00000000..236672e2 --- /dev/null +++ b/src/main/java/cn/lihongjie/coal/config/LogRequestBodyAdvice.java @@ -0,0 +1,38 @@ +package cn.lihongjie.coal.config; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.core.MethodParameter; +import org.springframework.http.HttpInputMessage; +import org.springframework.http.converter.HttpMessageConverter; +import org.springframework.web.bind.annotation.RestControllerAdvice; +import org.springframework.web.context.request.RequestAttributes; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdvice; + +import java.io.IOException; +import java.lang.reflect.Type; + +@RestControllerAdvice +@Slf4j +public class LogRequestBodyAdvice implements RequestBodyAdvice { + @Override + public boolean supports(MethodParameter methodParameter, Type targetType, Class> converterType) { + return true; + } + + @Override + public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class> converterType) throws IOException { + return inputMessage; + } + + @Override + public Object afterBodyRead(Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class> converterType) { + RequestContextHolder.currentRequestAttributes().setAttribute("__request_body", body, RequestAttributes.SCOPE_REQUEST); + return body; + } + + @Override + public Object handleEmptyBody(Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class> converterType) { + return null; + } +} diff --git a/src/main/java/cn/lihongjie/coal/dto/CoalBlendRequest.java b/src/main/java/cn/lihongjie/coal/dto/CoalBlendRequest.java index 9bdcf586..b23d5eca 100644 --- a/src/main/java/cn/lihongjie/coal/dto/CoalBlendRequest.java +++ b/src/main/java/cn/lihongjie/coal/dto/CoalBlendRequest.java @@ -3,6 +3,7 @@ package cn.lihongjie.coal.dto; import com.fasterxml.jackson.annotation.JsonIgnore; import lombok.Data; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.stream.Collectors; @@ -27,9 +28,15 @@ public class CoalBlendRequest { private int percent2Sum = 10; + public Map getConstraintMap() { if (constraintMap == null) { + if (constraints != null) { + constraintMap = constraints.stream().collect(Collectors.toMap(e -> e.getCode(), e -> e)); + }else { + constraintMap = new HashMap<>(); + } } return constraintMap; } diff --git a/src/main/java/cn/lihongjie/coal/service/CoalService.java b/src/main/java/cn/lihongjie/coal/service/CoalService.java index 6994f5f8..e18fddc9 100644 --- a/src/main/java/cn/lihongjie/coal/service/CoalService.java +++ b/src/main/java/cn/lihongjie/coal/service/CoalService.java @@ -1,6 +1,7 @@ package cn.lihongjie.coal.service; import cn.lihongjie.coal.dto.*; +import cn.lihongjie.coal.exception.BizException; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; @@ -33,6 +34,20 @@ public class CoalService { * @return */ public CoalBlendResult blend(CoalBlendRequest request) { + + if (CollectionUtils.isEmpty(request.getCoals())) { + throw new BizException("煤不能为空"); + } + + if (CollectionUtils.isEmpty(request.getConstraints())) { + throw new BizException("参数配置不能为空"); + } + + + + + + Loader.loadNativeLibraries(); CoalBlendResult result = new CoalBlendResult();