I believe that every developer has heard this sentence "Never trust user input", so the background needs to verify every input item of the user: mobile phone number, user name, password, email, ID number... At this time, the hibernate-Validator verification framework needs to be launched. The following introduces how springboot uses hibernate-Validator for verification.
Introduce pom
WAIT ~~~
starter-web dependency
Are you surprised or surprised? springboot has integrated it for us, let's just take it! Come! use!
Add annotation
@NotBlank(message = "The username cannot be empty") private String username; @DecimalMin(value = "6", message = "The password length cannot be less than 6 characters") @DecimalMax(value = "20", message = "The password length cannot be more than 20 characters") private String password;
Support regular expressions
Verify call
@ResponseBody @RequestMapping("save") public ResultModel<SysUser> save(@Valid SysUser sysUser, BindingResult result){ if (result.hasErrors()){ result.getAllErrors().forEach(error-> System.out.println(error.getDefaultMessage())); } return ResultModel.defaultSuccess(null); }Test results
1. Send a request
2. Operation results
Summarize
The above is the verification method of springboot using Validator introduced to you. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!