| | |
| | | import com.dy.common.webUtil.BaseResponse; |
| | | import com.dy.common.webUtil.BaseResponseUtils; |
| | | 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; |
| | |
| | | @RestControllerAdvice |
| | | @Slf4j |
| | | public class ExceptionHandlerAdvice { |
| | | |
| | | @ExceptionHandler(MethodArgumentNotValidException.class) |
| | | public BaseResponse<?> handleValidationExceptions(MethodArgumentNotValidException ex) { |
| | | log.error("[handleValidationExceptions]", ex); |
| | | StringBuilder sb = new StringBuilder(); |
| | | ex.getBindingResult().getAllErrors().forEach(error -> { |
| | | // String fieldName = ((org.springframework.validation.FieldError) error).getField(); |
| | | String errorMessage = error.getDefaultMessage(); |
| | | sb.append(errorMessage).append(";"); |
| | | }); |
| | | return BaseResponseUtils.buildException(sb.substring(0, sb.length() - 1)); |
| | | } |
| | | /** |
| | | * 处理系统异常,兜底处理所有异常 |
| | | */ |