@Slf4j
//RestControllerAdvice扫描有@Valid标记的必填字段所在的Controller
@RestControllerAdvice(basePackages = "com.aomalala.system.controller")
public class ExceptionControllerAdvice {
@ExceptionHandler(value = MethodArgumentNotValidException.class)
public Result handlerVaildException( MethodArgumentNotValidException e){
log.error("数据校验出现问题{}","异常类型:{}",e.getMessage(),e.getClass());
BindingResult bindingResult = e.getBindingResult();
List<Object> errorList = new ArrayList<>();
bindingResult.getFieldErrors().forEach(fieldError -> {
errorList.add(fieldError);
});
String defaultMessage = bindingResult.getFieldError().getDefaultMessage();
return Result.fail(500,defaultMessage);
}
}
@Valid注解校验必填字段返回错误信息
最新推荐文章于 2023-04-13 16:40:38 发布