Links
- Hibernate Validator Jakarta Bean Validation constraints
- Hibernate Validator Additional constraints
- 如何优雅的做数据校验-Hibernate Validator详细使用说明
pom.xml
Spring Boot Starter Validation
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-validation -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
Spring Boot 中其实不太存在表单验证这样一个概念,我们用 Bean Validation 来称呼它。
在 spring-boot-starter-web
中已经包含了 spring-boot-starter-validation
这个类库,所以我们不需要再手动添加依赖,以下提到的校验方法都可以直接使用。
Bean Validation 2.0 为现行校验标准,它定义了如下一些校验:
- @Null
- @NotNull
- @AssertTrue
- @AssertFalse
- @Min
- @Max
- @DecimalMin
- @DecimalMax
- @Negative
- @NegativeOrZero
- @Positive
- @PositiveOrZero
- @Size
- @Digits
- @Past
- @PastOrPresent
- @Future
- @FutureOrPresent
- @Pattern
- @NotEmpty
- @NotBlank
而 Hibernate Validator 作为它的最佳实现,同时又增加以下一些校验:
- @CreditCardNumber(ignoreNonDigitCharacters=)
- @Currency(value=)
- @DurationMax(days=, hours=, minutes=, seconds=, millis=, nanos=, inclusive=)
- @DurationMin(days=, hours=, minutes=, seconds=, millis=, nanos=, inclusive=)
- @EAN
- @ISBN
- @Length(min=, max=)
- @CodePointLength(min=, max=, normalizationStrategy=)
- @LuhnCheck(startIndex= , endIndex=, checkDigitIndex=, ignoreNonDigitCharacters=)
- @Mod10Check(multiplier=, weight=, startIndex=, endIndex=, checkDigitIndex=, ignoreNonDigitCharacters=)
- @Mod11Check(threshold=, startIndex=, endIndex=, checkDigitIndex=, ignoreNonDigitCharacters=, treatCheck10As=, treatCheck11As=)
- @Range(min=, max=)
- @SafeHtml(whitelistType= , additionalTags=, additionalTagsWithAttributes=, baseURI=)
- @ScriptAssert(lang=, script=, alias=, reportOn=)
- @UniqueElements
- @URL(protocol=, host=, port=, regexp=, flags=)