Spring 默认验证器与 Hibernate 验证器:哪个更好?

Spring default validator vs Hibernate validator : Which is better?

我的项目必须从 Spring MVC 迁移到 Spring boot,我们设法从 Spring Initializr 初始化 Spring 引导,它与 [=13= 捆绑在一起] 默认。我们之间有一个混淆选择 Spring default validatorHibernate validator。 请指教!!

经过一天的研究,我发现了一些事实。

截至目前 Spring Boot 1.5.9 默认带有 Bean Validation 1.0 (JSR-303) 和 Bean Validation 1.1 (JSR- 349)

  • Java Bean Validation (JSR 303) 是一个框架,该框架已于 2009 年 11 月获得 JCP 的批准,并被接受为 Java EE 6 规范的一部分。
  • Bean Validation 1.1 (JSR 349) 在 2013.It 中完成,是 Java EE 7 的一部分。

Hibernate Validator 6.X 另一方面是参考实现 Bean Validation 2.0 (JSR 380).

  • 支持 @Pastand@Future

  • 的 JSR 310 date/time 类型
  • 新的内置约束:@Positive@PositiveOrZero@Negative@NegativeOrZero@PastOrPresent@FutureOrPresent

  • Bean Validation 2.0 也将成为 Java EE 8 平台的一部分。

这是这两个内置约束的比较(点击Run code snippet查看注释比较

.w3-table,.w3-table-all{border-collapse:collapse;border-spacing:0;width:100%;display:table}.w3-table-all{border:1px solid #ccc}
.w3-bordered tr,.w3-table-all tr{border-bottom:1px solid #ddd}.w3-striped tbody tr:nth-child(even){background-color:#f1f1f1}
.w3-table-all tr:nth-child(odd){background-color:#fff}.w3-table-all tr:nth-child(even){background-color:#f1f1f1}
.w3-hoverable tbody tr:hover,.w3-ul.w3-hoverable li:hover{background-color:#ccc}.w3-centered tr th,.w3-centered tr td{text-align:center}
.w3-table td,.w3-table th,.w3-table-all td,.w3-table-all th{padding:8px 8px;display:table-cell;text-align:left;vertical-align:top}
.w3-table th:first-child,.w3-table td:first-child,.w3-table-all th:first-child,.w3-table-all td:first-child{padding-left:16px}
.red{
color : red;
}
.green{
color : green;
}
<table class="w3-table-all w3-card-4">
  <thead>
    <tr>
      <th>Annotations / Features</th>
      <th>Hibernate Validator 6.X</th>
      <th>Spring default Validator</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>@AssertFalse</td>
      <td class="green">Yes</td>
      <td class="green">Yes</td>
    </tr>
    <tr>
      <td>@AssertTrue</td>
      <td class="green">Yes</td>
      <td class="green">Yes</td>
    </tr>
    <tr>
      <td>@DecimalMax</td>
      <td class="green">Yes</td>
      <td class="green">Yes</td>
    </tr>
    <tr>
      <td>@DecimalMin</td>
      <td class="green">Yes</td>
      <td class="green">Yes</td>
    </tr>
    <tr>
      <td>@Digits</td>
      <td class="green">Yes</td>
      <td class="green">Yes</td>
    </tr>
    <tr>
      <td>@Future</td>
      <td class="green">Yes</td>
      <td class="green">Yes</td>
    </tr>
    <tr>
      <td>@Max</td>
      <td class="green">Yes</td>
      <td class="green">Yes</td>
    </tr>
    <tr>
      <td>@Min</td>
      <td class="green">Yes</td>
      <td class="green">Yes</td>
    </tr>
    <tr>
      <td>@NotNull</td>
      <td class="green">Yes</td>
      <td class="green">Yes</td>
    </tr>
    <tr>
      <td>@Null</td>
      <td class="green">Yes</td>
      <td class="green">Yes</td>
    </tr>
    <tr>
      <td>@Past</td>
      <td class="green">Yes</td>
      <td class="green">Yes</td>
    </tr>
    <tr>
      <td>@Pattern</td>
      <td class="green">Yes</td>
      <td class="green">Yes</td>
    </tr>
    <tr>
      <td>@Size</td>
      <td class="green">Yes</td>
      <td class="green">Yes</td>
    </tr>
    <tr>
    </tr>
    <tr>
      <td>@Negative</td>
      <td class="green">Yes</td>
      <td class="red">No</td>
    </tr>
    <tr>
      <td>@NegativeOrZero</td>
      <td class="green">Yes</td>
      <td class="red">No</td>
    </tr>
    <tr>
      <td>@Positive</td>
      <td class="green">Yes</td>
      <td class="red">No</td>
    </tr>
    <tr>
      <td>@PositiveOrZero</td>
      <td class="green">Yes</td>
      <td class="red">No</td>
    </tr>
    <tr>
      <td>@PastOrPresent</td>
      <td class="green">Yes</td>
      <td class="red">No</td>
    </tr>
    <tr>
      <td>@FutureOrPresent</td>
      <td class="green">Yes</td>
      <td class="red">No</td>
    </tr>
    <tr>
      <td>@NotEmpty</td>
      <td class="green">Yes</td>
      <td class="red">No</td>
    </tr>
    <tr>
      <td>@NotBlank</td>
      <td class="green">Yes</td>
      <td class="red">No</td>
    </tr>
    <tr>
      <td>@Email</td>
      <td class="green">Yes</td>
      <td class="red">No</td>
    </tr>



  </tbody>
</table>