Data check -hibernate-validator

Data check 
during web development, a request for a parameter, the parameter generally requires the legitimacy check, when writing a previous fields one to judge, in this way too general, so the java JSR 303: Bean Validation specification is to solve this problem. 

JSR 303 is just a specification, no specific implementation, the current is usually only hibernate-validator unified parameter check. 

Parity type defined JSR303 

Detailed the Constraint 
@null annotated element must be null 
@NotNull annotated element must not be null 
@AssertTrue annotated element must be true 
annotated element must @AssertFalse to false 
@min (value ) annotated element must be a number whose value must be greater than the specified minimum 
@Max (value) annotated element must be a number whose value is equal to the specified maximum value must be less than 
@DecimalMin (value) to be annotated must be a number of elements which must be greater than equal to the specified minimum value 
@DecimalMax (value) annotated element must be a number whose value is equal to the specified maximum value must be less than 
@Size (max, min) of the element to be annotated size must be within the specified range 
@Digits (integer, fraction) annotated element must be a number whose value must be within an acceptable range 
@Past annotated element must be a date in the past 
@Future element annotated It must be a future date
@Pattern (value) annotated elements must comply with the specified regular expression
Hibernate Validator additional constraint 

the Constraint details 
@Email annotated element must be the email address 
@Length string must be annotated within the specified size range 
@NotEmpty annotated non-empty string must 
@Range annotated element must be within the appropriate range 
to create entity classes 

@Data 

@NoArgsConstructor 

@AllArgsConstructor 

public class DemoReq { 

     

    @NotBlank (Message = "code can not be blank") 

    String code; 

    @Length (max = 10, Message = "name length can not exceed 10 ") 

    String name; 

} 

then the control layer method adding to @Valid, so before the access request parameters will be tested. 


@GetMapping ( "/ Demo / Valid") 

public String demoValid (@Valid DemoReq REQ) { 

    return req.getCode () + "," + req.getName (); 

}

  

Guess you like

Origin www.cnblogs.com/Struts-pring/p/10962036.html