Spring Validation动态校验规则

利用Spring validation的分组校验功能可以实现

同一个请求对象,同一份校验注解,不同接口,不同的校验逻辑。

User

1
2
3
4
5
6
7
class User{
//groups 指定适用规则
@Null(groups=Groups.Insert.class)
@NotNull()
private Long ID;
private String name;
}

GROUP

1
2
3
4
interface Groups{
interface Insert{}
interface Update{}
}

Controller

1
2
void methodA(@Validated(Groups.Insert.class) String xx)

gobal exception handler