Defining validations on the bean

Let's define a few validations on the Todo bean:

   public class Todo {     private int id;      @NotNull     private String user;     @Size(min = 9, message = "Enter atleast 10 Characters.")     private String desc;

Some important points to note are as follows:

  • @NotNull: Validates that the user field is not empty
  • @Size(min = 9, message = "Enter atleast 10 Characters."): Checks whether the desc field has at least nine characters

There are a number of other annotations that can be used to validate beans. The following are some of the Bean Validation annotations:

  • @AssertFalse, @AssertTrue: For Boolean elements. Checks the annotated element.
  • @AssertFalse: Checks for false. @Assert checks for true.
  • @Future: The annotated element ...

Get Mastering Spring 5.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.