源自:http://www.oschina.net/question/1241831_154710
jsr 303 如何控制错误信息的顺序(如按属性从上往下)
public interface GroupA { } public interface GroupB { } @GroupSequence({ GroupA.class, GroupB.class}) public interface Group { } public class User { @NotEmpty (message = "firstname may be empty") private String firstname; @NotEmpty(message = "middlename may be empty", groups = Default.class) private String middlename; @NotEmpty(message = "lastname may be empty",groups = GroupA.class) private String lastname; @NotEmpty(message = "country may be empty",groups = GroupB.class) private String country; } public static void main(String[] args){ User user = new User(); ValidatorFactory vf = Validation.buildDefaultValidatorFactory(); Validator validator = vf.getValidator(); Set<ConstraintViolation<User>> set = validator.validate(user,Group.class); for (ConstraintViolation<User> constraintViolation : set) { System.out.println(constraintViolation.getMessage()); } }
@GroupSequence这个注解里面就是用来验证排序的,最前的选验证;一旦有错误便不会继续验证下去了...