Search code examples
javaspring-bootspring-mvchibernate-validatorjavax.validation

@Valid (javax.validation.Valid) is not recursive for the type of list


Controller:

@RequestMapping(...)
public void foo(@Valid Parent p){
}
class Parent {
  @NotNull // javax.validation.constraints.NotNull
  private String name;
  List<Child> children;
}

class Child {
  @NotNull
  private String name;
}

This triggers @NotNull for Parent.name but doesn't check for Child.name. How to make it trigger. I tried List<@Valid Child> children; also annotate Child class with @Valid annotation, doesn't work. Please help.

parent = { "name": null } fails. name can't be null.

child = { "name": null } works.


Solution

  • Have you tried it like this:

    class Parent {
        @NotNull // javax.validation.constraints.NotNull
        private String name;
    
        @Valid
        List<Child> children;
    }