The difference between @NotNull, @NotBlank and @NotEmpty of commonly used validation annotations




Entity field validation @NotNull, @NotEmpty, @NotBlank

1.@NotNull

It cannot be null, but it can be empty. It is generally used in the non-null check of the basic data type of the Integer type, and the fields marked by it can use @size, @Max, @Min to control the size of the field value

2.@NotEmpty

Cannot be null, and the length must be greater than 0, generally used on collection classes or arrays

3.@NotBlank

It can only act on the received String type, pay attention tocan only, cannot be null, and after calling trim(), the length must be greater than 0, that is: there must be actual characters



  • Note that when using annotations such as @NotBlank, it must be used together with @valid, otherwise @NotBlank will not work.
  • A BigDecimal field using the field validation tag should be @NotNull.
  • When using @Length, it is generally used on the String type to control the maximum length limit of the field value.
  • When using @Range, it is generally used on the Integer type to control the size range of the field value.


As shown below:

insert image description here




For 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


Commonly used validation annotations


javax.validation.constraints.xxx

annotation illustrate
@Null Restriction can only be null
@NotNull limit must not be null
@AssertTrue limit must be true
@AssertFalse limit must be false
@Min(value) limit must be a number not less than the specified value
@Max(value) limit must be a number no greater than the specified value
@DecimalMin(value) The restricted element must be a number whose value must be greater than or equal to the specified minimum value
@DecimalMax(value) The restricted element must be a number whose value must be less than or equal to the specified maximum value
@Size(max,min) The limit character length must be within the specified range from min to max
@Digits(integer,fraction) The limit must be a decimal, and the number of digits in the integer part cannot exceed integer, and the number of digits in the fractional part cannot exceed fraction within an acceptable range
@Past The constrained element (of type Date) must be a date in the past
@Future The constrained element (of type date) must be a future date
@Pattern(value) Restrictions must match the specified regular expression
@Email The restricted element value is an email address, and a custom email can also be specified through regular expressions and flags
@Length The size of the restricted string must be within the specified range
@NotEmpty The restricted string must be non-empty (not null and not empty)
@Range The constrained element must be within the appropriate range


  • With @JsonFormat

Sometimes when the @JsonFormat annotation is used, the found time may be eight hours less than the time in the database. This is caused by the time zone difference. The default time zone of JsonFormat is Greenwich Time, and the default is Greenwich Mean Time, and we are On the East Eighth District, so the time will be eight hours less than the actual time we want. You need to add a time zone after it, as in the following example:


@JsonFormat(pattern="yyyy-MM-dd",timezone="GMT+8")
private Date date;

















Note:
Likes, comments, and reprints are welcome. Please give the link to the original text in an obvious place on the article page
. Those who know, thank you for reading my article in the vast crowd.
Where is the signature without personality!
For details, please follow me
and continue to update

Scan to have a surprise!
© 2020 11 - Guyu.com | 【All rights reserved】

Guess you like

Origin blog.csdn.net/weixin_49770443/article/details/109772162