Links


pom.xml

Spring Boot Starter Validation

  1. <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-validation -->
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-validation</artifactId>
  5. </dependency>

Spring Boot 中其实不太存在表单验证这样一个概念,我们用 Bean Validation 来称呼它。
spring-boot-starter-web 中已经包含了 spring-boot-starter-validation 这个类库,所以我们不需要再手动添加依赖,以下提到的校验方法都可以直接使用。
Bean Validation 2.0 为现行校验标准,它定义了如下一些校验:

而 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=)