File tree 1 file changed +13
-9
lines changed
1 file changed +13
-9
lines changed Original file line number Diff line number Diff line change @@ -333,13 +333,17 @@ Validator validate
333
333
具体使用情况如下:
334
334
335
335
``` java
336
- ValidatorFactory factory = Validation . buildDefaultValidatorFactory();
337
- Validator validator = factory. getValidator()
338
- PersonRequest personRequest = PersonRequest . builder(). sex(" Man22" )
339
- .classId(" 82938390" ). build();
340
- Set<ConstraintViolation<PersonRequest > > violations = validator. validate(personRequest);
341
- // 输出异常信息
342
- violations. forEach(constraintViolation - > System . out. println(constraintViolation. getMessage()));
336
+ /**
337
+ * 手动校验对象
338
+ */
339
+ @Test
340
+ public void check_person_manually() {
341
+ ValidatorFactory factory = Validation . buildDefaultValidatorFactory();
342
+ Validator validator = factory. getValidator();
343
+ PersonRequest personRequest = PersonRequest . builder(). sex(" Man22" )
344
+ .classId(" 82938390" ). build();
345
+ Set<ConstraintViolation<PersonRequest > > violations = validator. validate(personRequest);
346
+ violations. forEach(constraintViolation - > System . out. println(constraintViolation. getMessage()));
343
347
}
344
348
```
345
349
@@ -350,7 +354,7 @@ sex 值不在可选范围
350
354
name 不能为空
351
355
```
352
356
353
- ## 自定以 Validator(实用)
357
+ ## 自定义 Validator(实用)
354
358
355
359
如果自带的校验注解无法满足你的需求的话,你还可以自定义实现注解。
356
360
@@ -577,4 +581,4 @@ public class PersonService {
577
581
- ` @NotNull ` 是 JSR 303 Bean 验证批注,它与数据库约束本身无关。
578
582
- ` @Column(nullable = false) ` : 是 JPA 声明列为非空的方法。
579
583
580
- 总结来说就是即前者用于验证,而后者则用于指示数据库创建表的时候对表的约束。
584
+ 总结来说就是即前者用于验证,而后者则用于指示数据库创建表的时候对表的约束。
You can’t perform that action at this time.
0 commit comments