我正在使用Javax批注在Spring应用程序中收到请求时验证一堆字段。有些注释是默认的,有些是自定义的。我希望能够指定每个注释的执行顺序和提供错误消息的顺序。
以下是我使用这些注释的示例:
@Email(message = UserMessages.EMAIL_INVALID)
@UniqueEmail
private String email;
@NotEmpty(message = UserMessages.FIRSTNAME_NULL)
@Size(min = FirstnameLength_MIN, message = UserMessages.FIRSTNAME_TOO_SHORT)
@Size(max = FirstnameLength_MAX, message = UserMessages.FIRSTNAME_TOO_LONG)
private String firstName;
@NotEmpty(message = UserMessages.LASTNAME_NULL)
@Size(min = LastnameLength_MIN, message = UserMessages.LASTNAME_TOO_SHORT)
@Size(max = LastnameLength_MAX, message = UserMessages.LASTNAME_TOO_LONG)
private String lastName;
@NotEmpty(message = UserMessages.USERNAME_NULL)
@Size(min = UsernameLength_MIN, message = UserMessages.USERNAME_TOO_SHORT)
@Size(max = UsernameLength_MAX, message = UserMessages.USERNAME_TOO_LONG)
@NewUsername
private String username;
@NotEmpty(message = UserMessages.PASSWORD_NULL)
@Size(min = PasswordLength_MIN, message = UserMessages.PASSWORD_TOO_SHORT)
@Size(max = PasswordLength_MAX, message = UserMessages.PASSWORD_TOO_LONG)
private String password;
在理想情况下,我希望每个注释都按顺序进行验证,而不再进行任何操作。
我环顾四周,发现可以指定“组”来控制批注的验证顺序,但是我的印象是,该顺序只能应用于我创建的界面和验证者。如果我错了,请纠正我。
非常感谢!