约束可以与组进行关联,每个约束一定有group=Class>[]
元素,组是空的接口,作为指定强类型组的方式。利用组可以实现在运行验证过程中限定一组约束。
当启动验证过程时,我们可以通过Validator#validate(T object, Class> groups)
指定验证组。下面通过示例说明:
package com.dataz.groups;
public interface GroupName {
}
package com.dataz.groups;
public interface GroupAddress {
}
被验证实体类:
package com.dataz.entity;
import com.dataz.groups.GroupAddress;
import com.dataz.groups.GroupName;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
public class User {
@NotNull(groups = GroupName.class)
String firstName;
@NotNull(groups = GroupName.class)
String lastName;
@NotNull(groups = GroupAddress.class)
String streetAddress;
@NotNull(groups = GroupAddress.class)
String country;
@NotNull(groups = GroupAddress.class)
@Size(min = 5, groups = GroupAddress.class)
String zipCode;
@NotNull
String groupId;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getStreetAddress() {
return streetAddress;
}
public void setStreetAddress(String streetAddress) {
this.streetAddress = streetAddress;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getZipCode() {
return zipCode;
}
public void setZipCode(String zipCode) {
this.zipCode = zipCode;
}
public String getGroupId() {
return groupId;
}
public void setGroupId(String groupId) {
this.groupId = groupId;
}
}
使用一个组进行验证代码:
User user = new User();
user.setFirstName("Jennifer");
// user.setLastName("Wilson");
Set> constraintViolations =
validator.validate(user, GroupUserName.class);
if (constraintViolations.size() > 0) {
constraintViolations.stream().forEach(
ConstraintGroupExample::printError);
} else {
//proceed using user object
System.out.println(user);
}
输出:
lastName may not be null
使用两个组进行验证代码:
User user = new User();
user.setFirstName("Jennifer");
// user.setLastName("Wilson");
Set> constraintViolations =
validator.validate(user, GroupUserName.class,
GroupAddress.class);
if (constraintViolations.size() > 0) {
constraintViolations.stream().forEach(
ConstraintGroupExample2::printError);
} else {
//proceed using user object
System.out.println(user);
}
country may not be null
zipCode may not be null
lastName may not be null
streetAddress may not be null
不带任何做进行验证:
如果不指定组,默认为缺省组,示例中只有一个约束userId为缺省组。
User user = new User();
user.setFirstName(“Jennifer”);
// user.setLastName(“Wilson”);
Set
缺省情况,约束组验证没有特定顺序,但我们可以指定验证顺序。通过带注解@GroupSequence的接口实现。
该注解不仅指定顺序,如果有一个验证失败,则下一组约束将不被执行。这和前面的示例不同,因为之前是所有验证一起执行。
我们看如何定义组顺序接口:
import javax.validation.GroupSequence;
import javax.validation.groups.Default;
@GroupSequence({Default.class, GroupUserName.class, GroupAddress.class})
public interface GroupSequenceForUser {
}
现在将按照组顺序进行验证,第一组为默认组,如果没有错误,GroupUserName组进行验证,没有错误则GroupAddress组开始验证。
为达到效果,验证时传入顺序接口。
User user = new User();
Set
组顺序有个很好的特点,使用@GroupSequence
接口代替顺序接口,在验证实体上指定验证顺序注解。
@GroupSequence({User2.class, GroupUserName.class, GroupAddress.class})
public class User2 {
@NotNull(groups = GroupUserName.class)
String firstName;
@NotNull(groups = GroupUserName.class)
String lastName;
@NotNull(groups = GroupAddress.class)
String streetAddress;
@NotNull(groups = GroupAddress.class)
String country;
@NotNull(groups = GroupAddress.class)
@Size(min = 5, groups = GroupAddress.class)
String zipCode;
@NotNull
String userId;
// getters and setters
}
因为组不能循环依赖,所以不能增加缺省组,应使用被验证类自身作为缺省组。
验证代码如下:
User2 user = new User2();
Set> constraintViolations =
validator.validate(user);
if (constraintViolations.size() > 0) {
constraintViolations.stream().forEach(
RedefiningDefaultGroupExample::printError);
} else {
//proceed using user object
System.out.println(user);
}
userId may not be null
本文介绍了分组验证及分组验证顺序。在实际项目中相同字段在不同业务场景有不同验证规则,需使用分组规则来解决。