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. MyController.java:

@Controller
public class MyController {

  @Autowired
  private Validator standardValidator;

  @Autowired
  private Person person;

  @GetMapping("/")
  public String getItBound(ServletRequest request) {
    ServletRequestParameterPropertyValues paramMap = new ServletRequestParameterPropertyValues(
        request);
    System.err.println(standardValidator.getClass().getName());

    DataBinder binder = new DataBinder(person);
    binder.bind(paramMap);

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

    binder.addValidators(standardValidator);
    binder.validate();

    Errors result = binder.getBindingResult();

    result.getAllErrors().stream().forEach(System.err::println);

    return "myTemplate";
  }

Person.java:

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

  @NotNull
  private String name;

  @Autowired
  @NotNull
  @Valid
  private Address address;

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

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

  public void setStreet(String street) {
    address.setStreet(street);
  }

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

  public void setCity(String city) {
    address.setCity(city);
  }    
}

Address.java:

@Component
@RequestScope
public class Address {

  @NotNull
  private String street;

  @NotNull
  private String city;

 // getters and setters omited

Calling

http://localhost:8080/?name=MaxMusterman

in a browser results in the following log messages:

org.springframework.validation.beanvalidation.LocalValidatorFactoryBean
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.target.name,NotNull.name,NotNull.java.lang.String,NotNull]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [target.name,name]; arguments []; default message [name]]; default message [darf nicht null sein]
Field error in object 'target' on field 'address': rejected value [null]; codes [NotNull.target.address,NotNull.address,NotNull]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: 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