I'm trying to validate a DTO (formModel, whatever term you prefer) which is a bit layered (inheritance and dependencies to other classes).
Is it possible to get the following configuration of models to work:
public abstract class A {
@NotNull
private String fieldA1;
@NotNull
@Size(min = 2, max = 30)
private String fieldA2;
// ... and so on
}
public class B extends A {
@NotNull
private String fieldB1;
@NotNull
private Xyz fieldB2;
// and so on
}
public class Xyz {
@NotNull
private String fieldXyz1;
// etc..
}
Spring rest controller:
@PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity addCustomer(@RequestBody @Valid B customerDto, BindingResult bindingResult) {
// controller logic here
}
The problem is: only constraint annotations that seem to "work" are those on the parent class A. I submit a request with bad fields in the class A, bindingResult.hasErrors()
returns true. If I submit a request with bad fields in classes B or Xyz, bindingResult.hasErrors()
is false and the controller just goes on behaving like the request is ok.
Any ideas why?