我正在使用Hibernate和Spring Annotations进行大量验证,如下所示:
public class Account {
@NotEmpty(groups = {Step1.class, Step2.class})
private String name;
@NotNull(groups = {Step2.class})
private Long accountNumber;
public interface Step1{}
public interface Step2{}
}
Run Code Online (Sandbox Code Playgroud)
然后在控制器中调用它:
public String saveAccount(@ModelAttribute @Validated({Account.Step1.class}) Account account, BindingResult result) {
//some more code and stuff here
return "";
}
Run Code Online (Sandbox Code Playgroud)
但我想基于控制器方法中的一些逻辑来决定使用的组.有没有办法手动调用验证?有点像result = account.validate(Account.Step1.class)?
我知道创建自己的Validator类,但这是我想避免的,我宁愿只使用类变量本身的注释.