File tree Expand file tree Collapse file tree 1 file changed +13
-9
lines changed Expand file tree Collapse file tree 1 file changed +13
-9
lines changed Original file line number Diff line number Diff line change @@ -333,13 +333,17 @@ Validator validate
333333具体使用情况如下:
334334
335335``` 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()));
343347}
344348```
345349
@@ -350,7 +354,7 @@ sex 值不在可选范围
350354name 不能为空
351355```
352356
353- ## 自定以 Validator(实用)
357+ ## 自定义 Validator(实用)
354358
355359如果自带的校验注解无法满足你的需求的话,你还可以自定义实现注解。
356360
@@ -577,4 +581,4 @@ public class PersonService {
577581- ` @NotNull ` 是 JSR 303 Bean 验证批注,它与数据库约束本身无关。
578582- ` @Column(nullable = false) ` : 是 JPA 声明列为非空的方法。
579583
580- 总结来说就是即前者用于验证,而后者则用于指示数据库创建表的时候对表的约束。
584+ 总结来说就是即前者用于验证,而后者则用于指示数据库创建表的时候对表的约束。
You can’t perform that action at this time.
0 commit comments