使用aop和BindingResult进行参数验证
作者:互联网
1、在需要校验的参数名上面添加注解
2、在web层接收参数(参数前面使用@Valid进行标记,后面必须紧跟参数bindingResult,存储参数的错误信息)
3、使用aop进行校验信息统一处理
@Component
@Aspect
public class ParameterValidAop implements Ordered{
@Around("execution(* com.zxgeo.seplatform.directory.web.*Controller.*(..)) && args(..,bindingResult)")
public Object validateParam(ProceedingJoinPoint pjp, BindingResult bindingResult) throws Throwable {
if (bindingResult.hasErrors()){
//错误信息
StringBuffer sb = new StringBuffer();
List<FieldError> fieldErrors = bindingResult.getFieldErrors();
for (FieldError fieldError : fieldErrors) {
sb.append(fieldError.getField()+":"+fieldError.getDefaultMessage()+";");
}
throw new ProjectException(sb.toString());
}
return pjp.proceed();
}
@Override
public int getOrder() {
return 10;
}
}
标签:return,验证,StringBuffer,BindingResult,参数,aop,fieldError,sb,bindingResult 来源: https://www.cnblogs.com/cq-yangzhou/p/10929812.html