我可以覆盖一个JSR-303的验证注解 [英] can I override a jsr-303 validation annotation

查看:507
本文介绍了我可以覆盖一个JSR-303的验证注解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个测试象下面这样:

I have a test like below:

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();
    }

}
}

与下面的输出:结果
[ConstraintViolationImpl {interpolatedMessage ='P',的PropertyPath =善堂,rootBeanClass =类别Com .... validator.TestSizeAnnotation $ C,messageTemplate ='P'}]

不过,我预计该注释可以 @Size 覆盖,并会出现警告。结果
有没有办法来实现这一目标?

However, I expected that the annotation can @Size be overridden, and no warning will appear.
Is there a way to accomplish that?

编辑:我发现了一个错误似乎与这一点,但我运行4.2.0最终仍然得到上述行为。

I found a bug seems related to that, but I run 4.2.0 final and still get the above behavior.

推荐答案

覆盖验证注解实际上是不支持JSR-303。在子类覆盖的方法相反注释将被应用的累计的:从规范的第3.3节:

Overriding validation annotations is actually not supported for JSR-303. Instead annotations on overridden methods in the subclass will be applied cumulatively: From section 3.3 of the specification:


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.

这篇关于我可以覆盖一个JSR-303的验证注解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆