jsr-303 Parameter Verification-learning

Source: Internet
Author: User

1, what is it? JSR303 is a set of JavaBean parameter checksums that define a number of commonly used checksum annotations, such as:
-------------------------------------------------@NotNull (Message="the name cannot be empty")PrivateString userName; @Max (Value= -, message="age cannot exceed 90 years")Private intAge ; @Email (Message="Bad mailbox Format")PrivateString Email;--------------------------------------------------
Just like the above, add annotations to the properties of our JavaBean, and we can verify them when we need to verify them. 2, misunderstanding (with Hibernate ORM is related) JSR-303 is a sub-specification in JAVA EE 6, called the Bean Validation, the official reference implementation is Hibernate Validator.so I thought he had a relationship with Hibernate ... But he doesn't really have a relationship ....Hibernate Validator is just a reference implementation of Bean Validation, hibernate Validator provides all the built-in constraint implementations in the JSR-303 specification, plus some additional Constrai NT 3, Dependency import (Spring-boot)
<dependency>      <groupId>org.springframework.boot</groupId>      <artifactId> Spring-boot-starter-validation</artifactid></dependency>
4. Common annotations 4.1, empty check
@Null verifies whether the object is null@notnull to verify that the object is not NULL, cannot check for a string of length 0 @notblank checks if the constraint string is Null and if the length of the trim is greater than 0, only the string, and the front and back spaces are removed. @ Notempty checks whether the constraint element is null or empty.
4.2, Booelan Inspection
Truefalse
4.3. Length check
The @Size (min=, max=) verifies that the length of the object (array,collection,map,string) is within a given range @length (minString is Between Min and Max included.
4.4. Date Check
@Past verifies that the date and calendar objects are validated before the current time, the annotated element must be a past date @future verifies that the date and calendar object are validated after the current time, the annotated element must be a future date @Pattern verifies that a String object conforms to a regular expression rule, the annotated element conforms to the established regular expression, regexp: Regular expression Flags: Specifies an array of pattern.flag that represents the relevant options for the regular expression.
4.5. Numerical check
  recommended for use in Stirng,integer type, not recommended for type int, because the form value is "" cannot be converted to int, but can be converted to stirng to "", Integer for null@min verifies that the number and string objects are large equal to the specified value @max Verify that the size of number and string objects is equal to the value specified @decimalmax the value that is being labeled must not be greater than the maximum value specified in the constraint. The parameter of this constraint is a string representation of the maximum value defined by BigDecimal. Decimal Presence Precision @decimalmin The value to be dimensioned must be not less than the minimum specified in the constraint. The parameter of this constraint is a string representation of the minimum value defined by BigDecimal. Decimal Presence Precision @digits verifies that the composition of number and String is legitimate @digits (integer  =,fraction=) verifies whether the string is a number that conforms to the specified format, interger Specifies the integer precision, and fraction specifies the decimal precision. @Range (min  =, Max= = 10000 , Max=50000 , Message= "range.bean.wage") @Valid check the associated object recursively, if the associated object is a collection or an array, then the element is recursively checked, and if it is a map, the value part of it is verified. (whether to perform recursive validation) @CreditCardNumber credit card verification @email Verify that it is an e-mail address, and if it is null, validation is passed. @ScriptAssert (lang  =, script=, Alias=) @URL (protocol  =,host=, port=, regexp=, flags=) 
5. Case analysis
// 5.1. Entity Bean  Public class validatetestclass{     "reason information cannot be empty ")    Private String reason; // Order cancellation Reason      // get, set method, parameter construction method, parameterless construction method, tostring method omitted   ......}

//5.2. Control Public classvalidatetestclassvalidatetest{@RequestBody @RequestMapping (value="/test")    Public voidAddbook (@Valid validatetestclass validatetestclass,bindingresult result) {//Note: Here a @valid parameter must be next to a bindingresult parameter, or spring will throw an exception when the checksum fails       if(Result.haserrors ()) {List<ObjectError> errorlist =result.getallerrors ();  for(Objecterror error:errorlist) {System. out. println (Error.getdefaultmessage ()); }        }     }}

Forwarding Address: 78922520

jsr-303 parameter Check-learn (GO)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.