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;
}
}