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

发布于 2024-12-31 22:46:41 字数 1142 浏览 0 评论 0原文

我有一个如下所示的测试:

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=lst, rootBeanClass=class com....validator.TestSizeAnnotation$C, messageTemplate='P'}]

但是,我预计注释可以 < code>@Size 被覆盖,并且不会出现警告。
有办法实现吗?

编辑:我发现一个 bug 似乎与此相关,但我运行 4.2.0 Final并仍然得到上述行为。

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

with the following output:
[ConstraintViolationImpl{interpolatedMessage='P', propertyPath=lst, rootBeanClass=class com....validator.TestSizeAnnotation$C, messageTemplate='P'}]

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

空城之時有危險 2025-01-07 22:46:41

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.

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.
想念有你 2025-01-07 22:46:41

您可以通过 xml 配置来验证注释配置的验证:

http://docs.jboss.org/hibernate/validator/4.2/reference/en-US/html_single/#validator-xmlconfiguration

在您的情况下,如果您在validation.xml 文件中为 getList() 方法声明不同的验证(或不验证),它将覆盖 @Size 注释。

You can ovveride an annotation configured validation via xml configuration:

http://docs.jboss.org/hibernate/validator/4.2/reference/en-US/html_single/#validator-xmlconfiguration

In your case, if you declare a different validation (or no validation) for the getList() methid in the validation.xml file, it will override the @Size annotation.

苍暮颜 2025-01-07 22:46:41

只是为了记录,我找到了解决我的问题的方法:

public class TestSizeAnnotation
{
    public static void main(String[] args)
    {
        System.out.println("c " + 
                Validation.buildDefaultValidatorFactory().getValidator().validate(new C(), Default.class, SizeGroup2.class));
        System.out.println("p " + 
                Validation.buildDefaultValidatorFactory().getValidator().validate(new P(), Default.class, SizeGroup.class));
    }
    public static interface SizeGroup {}
    public static interface SizeGroup2 {}
    public static class P 
    {
        private List<String> lst = newArrayList("AA");
        @Size(max=0, message="P" , groups=SizeGroup.class)
        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", groups=SizeGroup2.class)
        public List<String> getLst()
        {
            return super.getLst();
        }

    }
}

Just for the records, I have found a bypass for my problem:

public class TestSizeAnnotation
{
    public static void main(String[] args)
    {
        System.out.println("c " + 
                Validation.buildDefaultValidatorFactory().getValidator().validate(new C(), Default.class, SizeGroup2.class));
        System.out.println("p " + 
                Validation.buildDefaultValidatorFactory().getValidator().validate(new P(), Default.class, SizeGroup.class));
    }
    public static interface SizeGroup {}
    public static interface SizeGroup2 {}
    public static class P 
    {
        private List<String> lst = newArrayList("AA");
        @Size(max=0, message="P" , groups=SizeGroup.class)
        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", groups=SizeGroup2.class)
        public List<String> getLst()
        {
            return super.getLst();
        }

    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文