是否可以覆盖 hibernate/javax 验证的类层次结构? 或者如何通过扩展类来覆盖参数验证?
使用@Valid
注释并在我的类中设置javax验证注释。我想要一个扩展类,其中验证被删除。
我发现 hibernate 验证循环遍历整个类层次结构,甚至在父类(super class)中检查验证。
我从使用 Spring MVC 设置的基于 Json 的 REST API 中使用它。
示例:
public class Parent {
protected Integer numChildren;
@NotNull(message = "NumChildren has to be set.")
@Size(min 1, max 10, message = "A partent has to have at least one kid.")
public Integer getNumChildren() {
return numChildren;
}
public void setNumChildren(Integer numChilds) {
this.numChildren = numChilds;
}
}
public class Child extends Parent {
// A child doesnt have to have kids, but can, so I want to override this parameter
// and remove the validation requirement, and make it just optional.
@Override
public Integer getNumChildren() {
return super.numChildren;
}
}
最诚挚的问候, 马库斯
最佳答案
Bean 验证不允许禁用/更改父类(super class)中的约束。约束总是组合在一起的。我想方法是通过组,例如 ParentChecks 组和 ChildChecks 组。将特定于子级的父级的约束分配给相应的组。然后,您可以将父项的默认组重新定义为 ParentChecks、_Parent_,将子项的默认组重新定义为 ChildChecks、Child。这应该可行。
关于java - 验证类层次结构覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15554128/