3

Previous to the introduction of @Repeatable in Java 8, multiple of the same annotation on a field was not allowed. For XML Bean Validation, the way around this was to create a nested @List annotation inside of a Constraint annotation. For example, as explained in this post, nested inside of javax.validation.constraints.NotNull, you have:

@interface List {

  NotNull[] value();
}

When using annotations directly in the java code (as is usually done), you can have multiple @NotNull annotations defined thusly:

@NotNull.List({@NotNull(groups=Foo.class, message="myObject may not be null for Foo"), 
               @NotNull(groups=Bar.class, message="myObject may not be null for Bar"})
private Object myObject;

However, I am trying to configure the validation in an xml file instead of in my code (which is less commonly done).

My xml bean validation looks like this:

<field name="myObject">
  <constraint annotation="javax.validation.constraints.NotNull">
    <message>myObject may not be null for Foo</message>
    <groups>
      <value>my.package.Foo</value>
    </groups>
  </constraint>
  <constraint annotation="javax.validation.constraints.NotNull">
    <message>myObject may not be null for Bar</message>
    <groups>
      <value>my.package.Bar</value>
    </groups>
  </constraint>
</field>

This isn't working for me... the validation simply doesn't happen when I do this.

I've looked for clues in the xsd: http://jboss.org/xml/ns/javax/validation/mapping/validation-mapping-1.0.xsd, but I'm not seeing a way to incorporate @NotNull.List.

What is the right way to use multiple of the same constraint in xml?

James Dunn
  • 8,064
  • 13
  • 53
  • 87

0 Answers0