Hibernate validator: Difference between @NotBlank and @NotEmpty

Recently i was teaching about using Hibernate Validator with Spring MVC. Because of that, i was checking the complete list of available annotations for bean properties.

Then, i discovered annotation @NotBlank, which was new for me because i only knew @NotEmpty and @NotNull. The documentation is clear to establish the difference between them:

  • @NotNull: Checks whether the value is not null, disregarding the content
  • @NotEmpty: Checks whether the value is not null nor empty. If it has just empty spaces, it will allow it as not empty
  • @NotBlank: Checks whether the value is not null nor empty, trimming the value first. It means that, it won’t allow just empty spaces

So, if you want to validate that a field is not null but also that it doesn’t has just empty spaces, but text, you should use @NotBlank.