My project have to migrate from Spring MVC
to Spring boot
,and we are managed to initialize Spring boot from Spring Initializr and it is bundled with spring validation
by default. There is a confusion among us to choose either Spring default validator
or Hibernate validator
.
Please advice!!
After a day's research I have found out a few facts.
As of now Spring Boot 1.5.9 by default comes with Bean Validation 1.0
(JSR-303) and Bean Validation 1.1
(JSR-349)
Hibernate Validator 6.X on the other hand is the reference implementation Bean Validation 2.0
(JSR 380).
Support for the JSR 310 date/time types for @Pastand@Future
New built-in constraints:@Positive
, @PositiveOrZero
, @Negative
, @NegativeOrZero
, @PastOrPresent
and @FutureOrPresent
Bean Validation 2.0 will also be part of the Java EE 8 platform.
Here is the comparison of built-in constraints of theese two(Click on Run code snippet
and see annotation wise comparison.)
.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>