使用下面统一异常处理的处理方法就可以提示
import com.butler.lib.common.base.BaseResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;import java.util.List;/*** @author */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {/*** 校验错误拦截处理** @param exception 错误信息集合* @return BaseResponse 错误响应,当HTTP响应状态码不为200时,使用该响应返回*/@ResponseStatus(HttpStatus.BAD_REQUEST)@ExceptionHandler(MethodArgumentNotValidException.class)private BaseResponse validateRequestException(MethodArgumentNotValidException exception) {BindingResult bindingResult = exception.getBindingResult();StringBuilder errorMsg = new StringBuilder();if (bindingResult.hasErrors()) {List<ObjectError> errors = bindingResult.getAllErrors();for (ObjectError objectError : errors) {FieldError fieldError = (FieldError) objectError;if (log.isDebugEnabled()) {log.error("Data check failure : object: {},field: {},errorMessage: {}",fieldError.getObjectName(), fieldError.getField(), fieldError.getDefaultMessage());}errorMsg.append(objectError.getDefaultMessage());errorMsg.append(",");}errorMsg = new StringBuilder(errorMsg.substring(0, errorMsg.length() - 1));}return new BaseResponse("ILLEGAL_ARGUMENT_ERROR", errorMsg.toString());}}