mirror of
https://codeup.aliyun.com/64f7d6b8ce01efaafef1e678/coal/coal.git
synced 2026-01-25 07:46:40 +08:00
全局异常处理
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<? extends HttpMessageConverter<?>> converterType) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) throws IOException {
|
||||
return inputMessage;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object afterBodyRead(Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> 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<? extends HttpMessageConverter<?>> converterType) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -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<String, CoalConstraint> getConstraintMap() {
|
||||
if (constraintMap == null) {
|
||||
if (constraints != null) {
|
||||
|
||||
constraintMap = constraints.stream().collect(Collectors.toMap(e -> e.getCode(), e -> e));
|
||||
}else {
|
||||
constraintMap = new HashMap<>();
|
||||
}
|
||||
}
|
||||
return constraintMap;
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user