-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 增加 DictValid3 适应 SpringBoot 3.x
- Loading branch information
Showing
5 changed files
with
68 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
src/main/java/com/houkunlin/system/dict/starter/json/DictValid3.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.houkunlin.system.dict.starter.json; | ||
|
||
import jakarta.validation.Constraint; | ||
import jakarta.validation.Payload; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* 字典校验,如果找不到字典值文本,则校验失败。 | ||
* SpringBoot 3.x 的校验用法。主要是 javax.validation 包名问题,SpringBoot 3.x 改包名了 | ||
* | ||
* @author HouKunLin | ||
*/ | ||
@Constraint(validatedBy = {DictValid3ConstraintValidator.class}) | ||
@Target({ElementType.METHOD, ElementType.FIELD}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface DictValid3 { | ||
String message() default "字典参数错误"; | ||
|
||
Class<?>[] groups() default {}; | ||
|
||
Class<? extends Payload>[] payload() default {}; | ||
|
||
/** | ||
* 数据字典的代码。 | ||
* 当此注解在系统字典枚举上时,该字段表示字典类型代码。 | ||
* | ||
* @return 数据字典代码 | ||
* @see DictText#value() | ||
*/ | ||
String value(); | ||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/com/houkunlin/system/dict/starter/json/DictValid3ConstraintValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.houkunlin.system.dict.starter.json; | ||
|
||
import com.houkunlin.system.dict.starter.DictUtil; | ||
import jakarta.validation.ConstraintValidator; | ||
import jakarta.validation.ConstraintValidatorContext; | ||
|
||
/** | ||
* 校验字典信息是否在字典列表中。校验字典信息的填写是否符合需求 | ||
* | ||
* @author HouKunLin | ||
*/ | ||
public class DictValid3ConstraintValidator implements ConstraintValidator<DictValid3, Object> { | ||
private String dictType; | ||
|
||
@Override | ||
public boolean isValid(final Object value, final ConstraintValidatorContext context) { | ||
if (dictType == null || value == null) { | ||
return false; | ||
} | ||
return DictUtil.getDictText(dictType, value.toString()) != null; | ||
} | ||
|
||
@Override | ||
public void initialize(final DictValid3 constraintAnnotation) { | ||
this.dictType = constraintAnnotation.value(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters