获取springboot注解校验详细异常信息
public ArreryResult addProduct(@Valid VoAddProduct addProduct,Errors errors) {
List<ObjectError> oes = errors.getAllErrors();
for (ObjectError oe : oes) {
String key = null;
String msg = null;
// 字段错误
if (oe instanceof FieldError) {
FieldError fe = (FieldError) oe;
key = fe.getField();// 获取错误验证字段名
logger.info(key);
} else {
// 非字段错误
key = oe.getObjectName();// 获取验证对象名称
logger.info(key);
}
// 错误信息
msg = oe.getDefaultMessage();
logger.info(msg);
return arreryResult = new ArreryResult(null, 0, "");
}
}
本文深入探讨了SpringBoot中使用@Valid注解进行参数校验的方法,详细解析了如何捕获并处理校验失败时的异常信息,通过遍历Errors对象获取所有错误,并区分字段级与对象级错误,最终返回具体的错误信息。
817

被折叠的 条评论
为什么被折叠?



