我可以覆盖jsr-303验证注释

osh*_*hai 9 java annotations hibernate bean-validation

我有一个如下测试:

public class TestSizeAnnotation
{
public static void main(String[] args)
{
    System.out.println(
            Validation.buildDefaultValidatorFactory().getValidator().validate(new C()));
}

public static class P 
{
    private List<String> lst = newArrayList("AA");
    @Size(max=0, message="P")
    public List<String> getLst()
    {
        return lst;
    }
    public void setLst(List<String> lst)
    {
        this.lst = lst;
    }
}
public static class C extends P 
{
    @Override
    @Size(max=5, message="C")
    public List<String> getLst()
    {
        return super.getLst();
    }

}
}
Run Code Online (Sandbox Code Playgroud)

具有以下输出:
[ConstraintViolationImpl{interpolatedMessage='P', propertyPath=lst, rootBeanClass=class com....validator.TestSizeAnnotation$C, messageTemplate='P'}]

但是,我希望可以@Size覆盖注释,并且不会出现警告.
有没有办法实现这一目标?

编辑:我发现一个错误似乎与此有关,但我运行4.2.0最终仍然得到上述行为.

Per*_*ion 15

实际上,JSR-303不支持覆盖验证注释.相反,子类中重写方法的注释将被累积应用:从规范的3.3节:

A constraint declaration can be placed on an interface. For a given class,
constraint declarations held on super- classes as well as interfaces are
evaluated by the Bean Validation provider. Rules are formally described in
Section 3.4.5.

The effect of constraint declarations is cumulative. Constraints declared
on a superclass getter will be validated along with any constraints defined
on an overridden version of the getter according to the Java Language
Specification visibility rules.