Hibernate-Validator framework completes data validation
When we implement registration, generally the front-end will add verification, and the front-end verification is very easy to be bypassed by those who are interested. So we must add the data validation function in the background:
here we will use the Hibernate-Validator framework to complete the data validation:
and the SpringBoot web launcher has integrated related dependencies:
What is Hibernate Validator
Hibernate Validator is an open source framework provided by Hibernate. It is very convenient to implement server-side data verification using annotations.
Official website: http://hibernate.org/validator/releases/
hibernate Validator is the reference implementation of Bean Validation. Hibernate Validator provides the implementation of all the built-in constraints in the JSR 303 specification, in addition to some additional constraints.
In daily development, Hibernate Validator is often used to validate bean fields, based on annotations, which is convenient, fast and efficient.
Bean verification annotation
Common notes are as follows:
Constraint | details |
---|---|
@Valid | The annotated element is an object, and all field values of this object need to be checked |
@Null | The annotated element must be null |
@NotNull | The annotated element must not be null |
@AssertTrue | The annotated element must be true |
@AssertFalse | The annotated element must be false |
@Min(value) | The annotated element must be a number, and its value must be greater than or equal to the specified minimum value |
@Max(value) | The annotated element must be a number, and its value must be less than or equal to the specified maximum value |
@DecimalMin(value) | The annotated element must be a number, and its value must be greater than or equal to the specified minimum value |
@DecimalMax(value) | The annotated element must be a number, and its value must be less than or equal to the specified maximum value |
@Size(max, min) | The size of the annotated element must be within the specified range |
@Digits (integer, fraction) | The annotated element must be a number, and its value must be within an acceptable range |
@Past | The annotated element must be a past date |
@Future | The annotated element must be a date in the future |
@Pattern(value) | The annotated element must conform to the specified regular expression |
The annotated element must be an email address | |
@Length | The size of the commented string must be within the specified range |
@NotEmpty | The commented string must be non-empty |
@Range | The annotated element must be in the appropriate range |
@NotBlank | The commented string must be non-empty |
@URL(protocol=,host=, port=,regexp=, flags=) | The commented string must be a valid url |
@CreditCardNumber | The annotated string must pass the Luhn verification algorithm, bank cards, credit cards and other numbers generally use Luhn to calculate legitimacy |
Add verification to User
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
We add annotations to some attributes of the User object:
@Table(name = "tb_user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Length(min = 4, max = 30, message = "用户名只能在4~30位之间")
private String username;// 用户名
@JsonIgnore
@Length(min = 4, max = 30, message = "密码只能在4~30位之间")
private String password;// 密码
@Pattern(regexp = "^1[35678]\\d{9}$", message = "手机号格式不正确")
private String phone;// 电话
private Date created;// 创建时间
@JsonIgnore
private String salt;// 密码的盐值
}
To modify the register method in the controller, you only need to add @Valid annotations to the User.
Use postMan to test, and then SpringMVC will automatically return an error message: