spring Spring JSR 303 Bean Validation @Valid usage to validate nested POJOs

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

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;
}


Got any spring Question?