Hibernate官方文档-https://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/
根据对象状态来重定义默认组序列,我在工作中遇到一个参数校验问题,比如需要根据一个参数值X来校验其他参数是否符合要求,要满足X的不同值都能达到校验效果,我实现了DefaultGroupSequenceProvider接口重定义了校验组序列
public class DiyGroupSequencyProvider implements DefaultGroupSequenceProvider<ConfigParam> {
@Override
public List<Class<?>> getValidationGroups(ConfigParam param) {
List<Class<?>> defaultGroupSequency = new ArrayList<>();
//这一步不能省略,否则会抛错
defaultGroupSequency.add(ConfigParam.class);
if (param!=null){
if ("xxx".equals(param.getMedium())){
defaultGroupSequency.add(ConfigParam.xxxGroup.class);
}else{
defaultGroupSequency.add(ConfigParam.ErrorGroup.class);
}
}
return defaultGroupSequency;
}
}
@Data
@AllArgsConstructor
@NoArgsConstructor
@GroupSequenceProvider(value = DiyGroupSequencyProvider.class)
public class ConfigParam {
private String medium;
@NotBlank(message = "栏目/频道名称不可为空", groups = {xxxGroup.class})
private String name;
}