| | |
| | | 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 |
| | |
| | | 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()); |