tj
2025-03-20 87a0ccb7ed3f0c9bfd856169ef03de136cd1047d
src/main/java/com/jsh/erp/exception/GlobalExceptionHandler.java
@@ -3,11 +3,14 @@
import com.alibaba.fastjson.JSONObject;
import com.jsh.erp.constants.ExceptionConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;
@Slf4j
@RestControllerAdvice
@@ -32,6 +35,19 @@
            return status;
        }
        // 这里针对校验org.springframework.web.bind.MethodArgumentNotValidException 异常的处置
        if (e instanceof MethodArgumentNotValidException) {
            status.put(ExceptionConstants.GLOBAL_RETURNS_CODE, 400);
            List<String> errorMessages = ((MethodArgumentNotValidException) e).getBindingResult().getFieldErrors()
                    .stream()
                    .map(fieldError -> fieldError.getField() + ": " + fieldError.getDefaultMessage())
                    .collect(Collectors.toList());
            // 以 JSON 数组形式返回
            status.put(ExceptionConstants.GLOBAL_RETURNS_MESSAGE, errorMessages);
            return status;
        }
        status.put(ExceptionConstants.GLOBAL_RETURNS_CODE, ExceptionConstants.SERVICE_SYSTEM_ERROR_CODE);
        status.put(ExceptionConstants.GLOBAL_RETURNS_DATA, ExceptionConstants.SERVICE_SYSTEM_ERROR_MSG);
        log.error("Global Exception Occured => url : {}, msg : {}", request.getRequestURL(), e.getMessage());