Differences between @NotEmpty, @NotBlank, @NotNull

@NotEmpty for collections 
@NotBlank for String 
@NotNull for primitives

 



There are only simple results, but I can't find any more specific content, so I went to the source code and found the following comments:

 

1. @NotEmpty

/** 
* Asserts that the annotated string, collection, map or array is not {@code null} or empty. 

* @author Emmanuel Bernard 
* @author Hardy Ferentschik 
*/

That is to say, the String class, Collection, Map, and array with @NotEmpty added cannot be null or have a length of 0 (the isEmpty() method of String, Collection, and Map).

2. @NotBlank

/** 
* Validate that the annotated string is not {@code null} or empty. 
* The difference to {@code NotEmpty} is that trailing whitespaces are getting ignored. 

* @author Hardy Ferentschik 
*/

“The difference to {@code NotEmpty} is that trailing whitespaces are getting ignored.” –> Unlike {@code NotEmpty}, trailing spaces are ignored, that is, Strings with pure spaces are also illegal. That's why it is said that @NotBlank is used for String.

3. @NotNull

/** 
* The annotated element must not be {@code null}. 
* Accepts any type. 

* @author Emmanuel Bernard 
*/

This is easy to understand, it cannot be null.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324774743&siteId=291194637