Linked Questions

Popular Questions

Spring Boot JSR-303 validator and request-scoped bean

Asked by At

Apparently I'm missing something about JSR-303 annotation-based validation in Spring Boot. I'm using Spring Boot 2.0.4.RELEASE.

Just to isolate the problem I have created a small "project" with two nested request-scoped beans and a controller. Even though some properties of the beans are set to valid values (and verified) I'm still getting validation error saying they all are null.

Here are my classes.

public class MyController {

  private Validator standardValidator;

  private Person person;

  public String getItBound(ServletRequest request) {
    ServletRequestParameterPropertyValues paramMap = new ServletRequestParameterPropertyValues(

    DataBinder binder = new DataBinder(person);

    System.err.println("person.getName() is " + person.getName());


    Errors result = binder.getBindingResult();


    return "myTemplate";

@RequestScope(proxyMode = ScopedProxyMode.TARGET_CLASS)
public class Person {

  private String name;

  private Address address;

  public String getName() {
    return name;

  public void setName(String name) { = name;

  public String getStreet() {
    return address.getStreet();

  public void setStreet(String street) {

  public String getCity() {
    return address.getCity();

  public void setCity(String city) {

public class Address {

  private String street;

  private String city;

 // getters and setters omited



in a browser results in the following log messages:

2019-02-01 12:40:02.453  INFO 11404 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 0 ms
person.getName() is MaxMusterman
Field error in object 'target' on field 'name': rejected value [null]; codes [,,,NotNull]; arguments [ codes [,name]; arguments []; default message [name]]; default message [darf nicht null sein]
Field error in object 'target' on field 'address': rejected value [null]; codes [,NotNull.address,NotNull]; arguments [ codes [target.address,address]; arguments []; default message [address]]; default message [darf nicht null sein]

So, although it the name property is set, the validator still complains as if were null.

Now, if I place the validation annotations on getter methods instead of the fields, the outer class instance (Person) is validated correctly. However, Address instance, which is a field in Person, is not validated at all, despite being annotated with @Valid.

OK, if I add a getter for Address (which I otherwise do not need), and annotate that as @Valid, it finally works.

But isn't it supposed to work with annotated fields? What am I missing here? Is this somehow related to proxying the "original" bean?

Related Questions