In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces SpringBoot how to verify the yue-library framework, the content is very detailed, interested friends can use for reference, I hope it can be helpful to you.
Validator
Lightweight server-side verification framework
Support for annotations, complete functions and easy to use
First, functional introduction
It mainly provides convenient background data verification function, supports single field or parameter verification, and also supports checking objects through annotations. It is easy to use.
Provide basic non-empty, length, size and other verification methods, but also provide some special regular check, ID card, telephone, mailbox, IP and other verification methods.
II. Introduction of notes
@ Valid the annotated element is a POJO object that checks whether the values of all annotated fields of this object are as expected.
Constraint built into Bean Validation
Annotation action @ Null annotated element must be null@NotNull annotated element must not be null@NotBlank annotated element must not be empty, and must contain at least one non-blank character @ NotEmpty annotated element must be non-empty @ AssertTrue annotated element must be true@AssertFalse annotated element must be false@Max annotated element must be a number Its value must be less than or equal to the specified maximum value @ Min annotated element must be a number, its value must be greater than or equal to the specified minimum value @ DecimalMax annotated element must be a number, its value must be less than or equal to the specified maximum value @ DecimalMin annotated element must be a number, its value must be greater than or equal to the specified minimum value @ Digits annotated element must be a number Its value must be within the acceptable range @ Positive annotated element must be strictly positive @ PositiveOrZero annotated element must be positive or 0@Negative annotated element must be a strictly negative number @ NegativeOrZero annotated element must be negative or 0@Past annotated element must be a moment in the past, date or time @ PastOrPresent annotated element must be a moment in the past or present. Date or time @ Future the annotated element must be a future moment, date, or time. @ FutureOrPresent the annotated element must be a current or future moment, date, or time. @ Size the size of the annotated element must be within the specified range @ Email the annotated element must be an email address @ Pattern the annotated element must conform to the specified regular expression
Constraint attached to Hibernate Validator
The size of the annotated string must be within the specified range @ Range the annotated element must be within the appropriate range @ URL verify that the annotated string is the URL@Currency currency amount must be in the correct monetary unit @ CreditCardNumber the annotated element must represent a valid credit card number @ CodePointLength verify that the code point length of the character sequence is between min and max @ constraint All constraints applied to the combined constraint annotation @ SafeHtml validate the user-supplied rich text value to ensure that it does not contain malicious code, such as the embedded element @ UniqueElements verifies that each object in the supplied collection is unique, that is, two equivalent elements @ EAN cannot be found in the collection to check whether the annotated character sequence is a valid EAN 13. Verify the length of the number and check the number @ ISBN to see if the annotated character sequence is a valid ISBN. The length of the number and the check number are verified @ LuhnCheckLuhn algorithm check constraint @ Mod10CheckModulo 10 check constraint @ Mod11CheckModulo 11 check constraint @ ParameterScriptAssert method-level constraint, which evaluates the script expression @ ScriptAssert class-level constraint according to the annotated method or constructor, and evaluates the script expression based on the annotated element
Constraint attached to Yue Validator
Annotation @ Cellphone Mobile number check @ IdCard ID Card check @ PlateNumber China license Plate @ Birthday Birthday check @ Chinese Chinese check @ English English check @ IPV4IPV4 address check @ IPV6IPV6 address check 3, Parameter Verification single Parameter Verification
Single parameter verification
/ / Parameter String name = validationIPO.getName (); String email = validationIPO.getEmail (); String cellphone = validationIPO.getCellphone (); int age = validationIPO.getAge (); DateTime birthday = validationIPO.getBirthday (); / / get the parameter verifier-Bean injection import ai.yue.library.base.validation.Validator;@Autowiredprivate Validator validator;// to get the parameter verifier-static method Validator.getValidatorAndSetParam (email) .email ("email") / / single parameter verification validator.param (email) .email ("email"); validator.param (cellphone) .cellphone ("cellphone"); validator.param (name) .notNull ("name"). Chinese ("name"). Length (1,30, "name")
Single parameter check-write via param () (switch check object directly)
Validator.param (name) .notNull ("name") .param (email) .length (5,25, "email") .param (age) .min (20, "age") .max (60, "age"); POJO object check (recommended)
Define annotations on the properties of the class and support custom error messages
@ Datapublic class ValidationIPO {@ NotEmpty (message = "name cannot be empty") @ Length (max = 20, message = "name cannot exceed 20 words") private String name; private DateTime birthday; @ IdCard private String idcard; @ Max (30) @ Min (12) private int age; @ Email @ Length (max = 50) private String email; @ Cellphone private String cellphone @ Pattern (regexp = "[1-9] ([0-9] {5pm 11})") private String qq;}
Method 1: Controller layer, checked by @ Valid (recommended)
@ PostMapping ("/ valid") public Result valid (@ Valid ValidationIPO validationIPO) {
Method 2: by calling the validator.valid () method
Validator.valid (validationIPO); / / also supports concatenating validator.valid (validationIPO) .param (birthday) .writing ("birthday").
Method 3: add the @ Valid annotation to the POJO class
@ Data@Validpublic class ValidationIPO {/ /.}
And receive this entity parameter at the Controller layer
@ PostMapping ("/ valid") public Result valid (ValidationIPO validationIPO) {handle when the check fails
A ValidateException (runtime exception) is thrown if the verification fails, which is handled by default by the ResultExceptionHandler class.
Example of error response:
{"code": 433, "msg": "Parameter verification failed Please check with API and try again, "flag": false, "count": null, "data": [{"errorkey": "cellphone", "errorValue": null, "errorHintMsg": "not a legitimate mobile phone number"}, {"errorkey": "idcard", "errorValue": "500223199607125633" "errorHintMsg": "not a legal ID number"} about how SpringBoot verifies the yue-library framework, that's all. I hope the above content can be of some help to you and learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.