JSR303 back-end verification
- What is JSR303?
- Import the jar package
- Validation rules
- Add annotations to properties
- Add the @valid annotation
What is JSR303?
JSR 303-Bean Validation provides support for back-end data Validation, which is important because if a one-click F12 modification of the front end code successfully bypassed the front end Validation, illegal data will be stored. Should be front-end + back-end + database check constraints can not be less, fully ensure the safety of data specifications.
Import the jar package
Importing dependencies in POM.xml without understanding Maven canClick here.
<! Hibernate </groupId> <artifactId> Hibernate -validator</artifactId> <version>5.1. 0.Final</version> </dependency> <! -- Tomcat7 if the following version of the EL expression is not compatible, <dependency> <groupId>javax.el</groupId> <artifactId>javax.el </artifactId> <version>3.0. 0</version>
</dependency>
Copy the code
Validation rules
Constraint | The detailed information |
---|---|
@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 whose value must be greater than or equal to the specified minimum |
@Max(value) | The annotated element must be a number whose value must be less than or equal to the specified maximum |
@DecimalMin(value) | The annotated element must be a number whose value must be greater than or equal to the specified minimum |
@DecimalMax(value) | The annotated element must be a number whose value must be less than or equal to the specified maximum |
@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 future date |
@Pattern(value) | The annotated element must conform to the specified regular expression |
The annotated element must be an E-mail address | |
@Length | The size of the annotated string must be within the specified range |
@NotEmpty | The value of the annotated string must be non-empty |
@Range | The annotated element must be in the appropriate scope |
The above is all the verification rules provided by the official website. The @pattern is commonly used and you can customize the rules.
Add annotations to properties
Annotate JavaaBean properties where message sets the error message, optionally leaving the default.
public class User {
@notnull (message="×: account cannot be empty ")
private Integer uid;
@NotNull
@length (min=6, Max =20,message="×: password Length must be 6-20")
private String password;
@ the Pattern (regexp = "(^ [a - zA - Z0 - _ - 9] 3 dec} {$) | (^ [\ u2E80 - \ u9FFF] {2, 5})", the message = "x: user called 2-5 3-16 English or Chinese digital combination")
private String name;
/ / @ the Pattern (regexp = "^ ([a - zA - Z0-9 _ \ \. -] +) @ ([\ \. \ \ da - z -] +) \ \. ([a-z \ \] {2, 6}) $"
//,message="×: mailbox format error ")
@Email
private String email;
@Past
private Date birthday;
Copy the code
Add the @valid annotation
Add the @VALID annotation to the Controller method argument, and the error message will be placed in the object argument of BindingResult.
@PostMapping("/user")
@ResponseBody
public Msg saveUser(@Valid User user,BindingResult result){
if (result.hasErrors()){
Map<String,Object> map = new HashMap<>();
// Verification failed, should return failure, in the modal box display verification failed error message
List<FieldError> errors = result.getFieldErrors();
for (FieldError fieldError:errors){
System.out.println("Error field name:"+fieldError.getField());
System.out.println(Error message:+fieldError.getDefaultMessage());
map.put(fieldError.getField(),fieldError.getDefaultMessage());
}
return Msg.fail().add("errorFields",map);
}else {
userService.saveUser(user);
returnMsg.success(); }}Copy the code
Original is not easy, please do not reprint (this is not rich visits add insult to injury) blogger home page: blog.csdn.net/qq_45034708 If the article is helpful to you, remember to focus on the likes collection ❤