gpt4 book ai didi

java - 验证类层次结构覆盖

转载 作者:行者123 更新时间:2023-12-01 04:47:43 26 4
gpt4 key购买 nike

是否可以覆盖 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_,将子项的默认组重新定义为 ChildChecksChild。这应该可行。

关于java - 验证类层次结构覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15554128/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com