当前位置:网站首页>The difference between @notnull, @notblank, @notempty of commonly used verification annotations
The difference between @notnull, @notblank, @notempty of commonly used verification annotations
2022-07-28 15:11:00 【Guyu γ】
Entity field verification @NotNull、@NotEmpty、@NotBlank
Not for null, But it can be empty, Generally used in Integer On the non empty check of the basic data type of type , And the fields marked by it can use @size、@Max、@Min Control the size of field values
Not for null, And the length must be greater than 0, Generally used on collection classes or arrays
It can only act on the received String On type , Note that Can only , Not for null, And call trim() after , The length must be greater than 0 namely : There must be actual characters
- Pay attention to use @NotBlank When waiting for comments , Be sure to and @valid Use it together , otherwise @NotBlank It doesn't work .
- One BigDecimal The field verification label used by the field should be @NotNull.
- In the use of @Length Generally used in String In terms of type, the maximum length limit of field value can be controlled .
- In the use of @Range Generally used in Integer In terms of type, you can control the size range of field values .
As shown in the figure below :

The following example :
1.String name = null;
@NotNull: false
@NotEmpty:false
@NotBlank:false
2.String name = "";
@NotNull:true
@NotEmpty: false
@NotBlank: false
3.String name = " ";
@NotNull: true
@NotEmpty: true
@NotBlank: false
4.String name = "Hello World!";
@NotNull: true
@NotEmpty:true
@NotBlank:true
Common verification notes
javax.validation.constraints.xxx
| annotation | explain |
|---|---|
| @Null | The limit can only be null |
| @NotNull | The limit must not be null |
| @AssertTrue | The limit must be true |
| @AssertFalse | The limit must be false |
| @Min(value) | The limit must be a number not less than the specified value |
| @Max(value) | The limit must be a number no greater than the specified value |
| @DecimalMin(value) | The restricted element must be a number , Its value must be greater than or equal to the specified minimum value |
| @DecimalMax(value) | The restricted element must be a number , Its value must be less than or equal to the specified maximum value |
| @Size(max,min) | The limit character length must be in min To max Within the specified range |
| @Digits(integer,fraction) | The limit must be a decimal , And the number of digits in the integer part cannot exceed integer、 The number of decimal places cannot exceed fraction Within the acceptable range |
| @Past | Restricted elements ( The date type ) It must be a date in the past |
| @Future | Restricted elements ( The date type ) It must be a future date |
| @Pattern(value) | The restriction must conform to the specified regular expression |
| The restricted element value is email address , You can also use regular expressions and flag Specify custom email | |
| @Length | The size of the restricted string must be within the specified range |
| @NotEmpty | The restricted string must be non empty ( Not for null It's not empty ) |
| @Range | The restricted elements must be within the appropriate range |
- attach @JsonFormat
Sometimes use @JsonFormat When the annotation , The search time may be eight hours less than that in the database , This is caused by the time zone difference ,JsonFormat The default time zone is Greenwich Time, The default is Greenwich mean time , And we're in the East eighth District , So the time will be eight hours less than we actually want . You need to add a time zone after it , The following example :
@JsonFormat(pattern="yyyy-MM-dd",timezone="GMT+8")
private Date date;
Note:
Welcome to thumb up , Leaving a message. , Reprint please give the link of the original text in the obvious position of the article page
The knower , Thank you for reading my article in the vast crowd
No personality Where did you get the signature !
Please pay attention to Am I
Ongoing update

| 2020 11 - Guyu.com | 【 copyright Infringement must be investigated 】 |
边栏推荐
- Introduction to mqtt protocol
- Iframe tag
- 2021 year end summary of gains and losses
- Pytorch GPU installation
- Node.js+express realizes the operation of MySQL database
- C language exercises
- 5、 C language judgment statement
- 3559. 围圈报数
- Xiaobai can understand the 35 necessary questions in MySQL interview
- The automatic prompt of vs code code is missing - a tick to solve it
猜你喜欢

Shell command

Instant experience | further improve application device compatibility with cts-d

Solution to the problem of high collapse caused by float (including all methods)

The second 1024, come on!

JS -- realize the rotation chart (complete function)

6、 C language circular statement

Enterprise wechat customer service link, enterprise wechat customer service chat

Machine learning related concepts

Image steganography method

PHP memory horse
随机推荐
[complete installation package & tutorial] sqlserver basic installation_ Sqlserver completely uninstalled_ Sqlserver custom installation_ Getting started with sqlserver_ SQLSERVER database
19、 ROS parameter name setting
4518. 最低票价
QT qbuttongroup realizes single selection and multiple selection
使用cpolar发布树莓派网页(apache2网页的发布)
15、 Launch file label of ROS (I)
Rocky基础之修改网卡名为eth0
Three pain points of software development! How to solve the applet container?
Deploy flask on Alibaba cloud server
PS how to crop photos
16、 Launch file label of ROS (II)
When MySQL uses left join to query tables, the query is slow because the connection conditions are not properly guided
MLX90640 红外热成像仪传感器模块开发笔记(八)
What are the CCSP cloud security design principles
Crawler: from entry to imprisonment (II) -- Web collector
使用cpolar发布树莓派网页(apache2的安装测试)
Using keras to visualize the network model, failed to import pydot appears
Development status of security and privacy computing in China
C language exercises
Establishment and traversal of binary tree (implemented in C language)