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

时间:2017-11-22 14:12:49

标签: spring-boot hibernate-validator spring-validator

我的项目必须从Spring MVC迁移到Spring boot,我们设法从Spring Initializr初始化Spring启动,默认情况下它与spring validation捆绑在一起。我们之间存在混淆,无法选择Spring default validatorHibernate validator。 请指教!!

1 个答案:

答案 0 :(得分:1)

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

截至目前 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年完成。它是Java EE 7的一部分。
另一方面,

Hibernate Validator 6.X 是参考实现Bean Validation 2.0(JSR 380)。

  • 支持@ Pastand @ Future的JSR 310日期/时间类型

  • 新的内置约束:@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>