我处理异常的方式如下所示,在这种情况下,我找到了特定的异常,然后创建了自己的类对象ValidationErrorDTO,然后填充该类中的必填字段(ValidationErrorDTO):
@ExceptionHandler(HttpMessageNotReadableException.class) @ResponseStatus(HttpStatus.BAD_REQUEST) @ResponseBody public ResponseEntity<ValidationErrorDTO> processValidationIllegalError(HttpMessageNotReadableException ex, HandlerMethod handlerMethod, WebRequest webRequest) { Throwable throwable = ex.getMostSpecificCause(); ValidationErrorDTO errorDTO = new ValidationErrorDTO(); if (throwable instanceof EnumValidationException) { EnumValidationException exception = (EnumValidationException) ex.getMostSpecificCause(); errorDTO.setEnumName(exception.getEnumName()); errorDTO.setEnumValue(exception.getEnumValue()); errorDTO.setErrorMessage(exception.getEnumValue() + " is an invalid " + exception.getEnumName()); } return new ResponseEntity<ValidationErrorDTO>(errorDTO, HttpStatus.BAD_REQUEST); }


