no message
Showing
3 changed files
with
67 additions
and
0 deletions
| 1 | package com.lego.common.annotation; | ||
| 2 | |||
| 3 | /** | ||
| 4 | * @author chentao | ||
| 5 | * @date 2025/4/23 | ||
| 6 | */ | ||
| 7 | |||
| 8 | import javax.validation.Constraint; | ||
| 9 | import javax.validation.Payload; | ||
| 10 | import java.lang.annotation.ElementType; | ||
| 11 | import java.lang.annotation.Retention; | ||
| 12 | import java.lang.annotation.RetentionPolicy; | ||
| 13 | import java.lang.annotation.Target; | ||
| 14 | @Constraint(validatedBy = AtLeastOneNonNullValidator.class) | ||
| 15 | @Target({ElementType.TYPE}) | ||
| 16 | @Retention(RetentionPolicy.RUNTIME) | ||
| 17 | public @interface AtLeastOneNonNull { | ||
| 18 | String message() default "At least one field must be non-null"; | ||
| 19 | Class<?>[] groups() default {}; | ||
| 20 | Class<? extends Payload>[] payload() default {}; | ||
| 21 | String[] fields(); // 指定需要校验的字段名数组 | ||
| 22 | } | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
| 1 | package com.lego.common.annotation; | ||
| 2 | |||
| 3 | import com.lego.common.annotation.AtLeastOneNonNull; | ||
| 4 | |||
| 5 | import javax.validation.ConstraintValidator; | ||
| 6 | import javax.validation.ConstraintValidatorContext; | ||
| 7 | import java.lang.reflect.Field; | ||
| 8 | import java.util.Arrays; | ||
| 9 | import java.util.List; | ||
| 10 | import java.util.Objects; | ||
| 11 | |||
| 12 | /** | ||
| 13 | * @author chentao | ||
| 14 | * @date 2025/4/23 | ||
| 15 | */ | ||
| 16 | public class AtLeastOneNonNullValidator implements ConstraintValidator<AtLeastOneNonNull, Object> { | ||
| 17 | private List<String> fields; | ||
| 18 | |||
| 19 | @Override | ||
| 20 | public void initialize(AtLeastOneNonNull constraintAnnotation) { | ||
| 21 | fields = Arrays.asList(constraintAnnotation.fields()); | ||
| 22 | } | ||
| 23 | |||
| 24 | @Override | ||
| 25 | public boolean isValid(Object object, ConstraintValidatorContext context) { | ||
| 26 | if (object == null) { | ||
| 27 | return false; // 如果对象本身为null,则不满足条件 | ||
| 28 | } | ||
| 29 | for (String fieldName : fields) { | ||
| 30 | try { | ||
| 31 | Field field = object.getClass().getDeclaredField(fieldName); | ||
| 32 | field.setAccessible(true); // 设置私有字段可访问,以便读取值 | ||
| 33 | if (Objects.nonNull(field.get(object))) { // 检查字段是否非空 | ||
| 34 | return true; // 如果至少一个字段非空,返回true | ||
| 35 | } | ||
| 36 | } catch (NoSuchFieldException | IllegalAccessException e) { | ||
| 37 | // 处理异常,例如记录日志等 | ||
| 38 | return false; // 出现异常时,保守起见返回false,实际中应更细致处理异常情况 | ||
| 39 | } | ||
| 40 | } | ||
| 41 | return false; // 所有指定字段均为空或出现异常,返回false | ||
| 42 | } | ||
| 43 | } |
| 1 | package com.lego.core.domin.bo; | 1 | package com.lego.core.domin.bo; |
| 2 | 2 | ||
| 3 | import com.lego.common.annotation.AtLeastOneNonNull; | ||
| 3 | import lombok.Data; | 4 | import lombok.Data; |
| 4 | 5 | ||
| 5 | import javax.validation.constraints.NotBlank; | 6 | import javax.validation.constraints.NotBlank; |
| ... | @@ -12,6 +13,7 @@ import javax.validation.constraints.Size; | ... | @@ -12,6 +13,7 @@ import javax.validation.constraints.Size; |
| 12 | * @date 2024/9/8 16:29 | 13 | * @date 2024/9/8 16:29 |
| 13 | */ | 14 | */ |
| 14 | @Data | 15 | @Data |
| 16 | @AtLeastOneNonNull(fields = {"twoDimensionalOssId", "threeDimensionalOssId"}) | ||
| 15 | public class CourseBo extends BaseBO { | 17 | public class CourseBo extends BaseBO { |
| 16 | private static final long serialVersionUID = 1L; | 18 | private static final long serialVersionUID = 1L; |
| 17 | 19 | ... | ... |
-
Please register or sign in to post a comment