Suppose we have a POJO class User we need to validate.
public class User {
@NotEmpty
@Size(min=5)
@Email
private String email;
}
and a controller method to validate the user instance
public String registerUser(@Valid User user, BindingResult result);
Let's extend the User with a nested POJO Address we also need to validate.
public class Address {
@NotEmpty
@Size(min=2, max=3)
private String countryCode;
}
Just add @Valid
annotation on address field to run validation of nested POJOs.
public class User {
@NotEmpty
@Size(min=5)
@Email
private String email;
@Valid
private Address address;
}