Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
282 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...