springboot使用hibernate validator

Foreword

 In development often need to write some field validation code, such as non-empty field, field length limit, the mailbox format validation and so on, to write these little relationship with the business logic code that personal feeling there are two problems:

  • Validation code tedious duplication of effort
  • Methods within the code is more verbose
  • Each time depends on which parameter validation is complete, you need to read the code validation logic

hibernate validator ( official document ) provides a more complete and convenient authentication implementation.

spring-boot-starter-webPacket inside hibernate-validatorthe package, no reference hibernate validator dependent.

A common comment

  Bean Validation built-in constraint A     
@null annotated element must be null     
@NotNull annotated element must not be null     
@AssertTrue annotated element must be to true     
@AssertFalse annotated element must be false     
@min (value) is a comment must be a number of elements which must be greater than equal to the specified minimum value     
@Max (value) must be annotated element is a number whose value is equal to the specified maximum value must be less than     
@DecimalMin (value) must be the element being annotated a number 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 =) sized element annotated 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 elements must be annotated is a future date     
@Pattern (regex =, flag =) annotated element must match the specified regular expression     
Hibe rnate Validator additional constraint     
@NotBlank (message =) authentication string is not null, and the length must be greater than 0     
@email must be annotated element is an electronic mail address     
@Length (min =, max =) annotated string size must be within the specified range     
@NotEmpty the annotation string must not be null     
@Range (min =, max =, message =) elements must be annotated in the range of appropriate

Two, hibernate validator calibration demo

 

Three, hibernate mode check

Four, hibernate two check

V. custom validator

VI. References

References:

        Transfer: https: //www.cnblogs.com/mr-yang-localhost/p/7812038.html

        Reference: http: //docs.jboss.org/hibernate/validator/4.2/reference/zh-CN/html_single/#validator-gettingstarted

Guess you like

Origin www.cnblogs.com/tanouou/p/12073113.html