no message
Showing
3 changed files
with
10 additions
and
29 deletions
| 1 | package com.lego.common.annotation; | 1 | package com.lego.core.annotation; |
| 2 | 2 | ||
| 3 | /** | 3 | /** |
| 4 | * @author chentao | 4 | * @author chentao |
| 5 | * @date 2025/4/23 | 5 | * @date 2025/4/23 |
| 6 | */ | 6 | */ |
| 7 | 7 | ||
| 8 | import com.lego.core.annotation.AtLeastOneNonNullValidator; | ||
| 9 | |||
| 8 | import javax.validation.Constraint; | 10 | import javax.validation.Constraint; |
| 9 | import javax.validation.Payload; | 11 | import javax.validation.Payload; |
| 10 | import java.lang.annotation.ElementType; | 12 | import java.lang.annotation.ElementType; |
| ... | @@ -15,7 +17,7 @@ import java.lang.annotation.Target; | ... | @@ -15,7 +17,7 @@ import java.lang.annotation.Target; |
| 15 | @Target({ElementType.TYPE}) | 17 | @Target({ElementType.TYPE}) |
| 16 | @Retention(RetentionPolicy.RUNTIME) | 18 | @Retention(RetentionPolicy.RUNTIME) |
| 17 | public @interface AtLeastOneNonNull { | 19 | public @interface AtLeastOneNonNull { |
| 18 | String message() default "At least one field must be non-null"; | 20 | String message() default "2D和3D不能都为空"; |
| 19 | Class<?>[] groups() default {}; | 21 | Class<?>[] groups() default {}; |
| 20 | Class<? extends Payload>[] payload() default {}; | 22 | Class<? extends Payload>[] payload() default {}; |
| 21 | String[] fields(); // 指定需要校验的字段名数组 | 23 | String[] fields(); // 指定需要校验的字段名数组 | ... | ... |
| 1 | package com.lego.common.annotation; | 1 | package com.lego.core.annotation; |
| 2 | 2 | ||
| 3 | import com.lego.common.utils.StringUtils; | 3 | import com.lego.common.utils.StringUtils; |
| 4 | import com.lego.core.domin.bo.CourseBo; | ||
| 4 | 5 | ||
| 5 | import javax.validation.ConstraintValidator; | 6 | import javax.validation.ConstraintValidator; |
| 6 | import javax.validation.ConstraintValidatorContext; | 7 | import javax.validation.ConstraintValidatorContext; |
| 7 | import java.lang.reflect.Field; | ||
| 8 | import java.util.Arrays; | 8 | import java.util.Arrays; |
| 9 | import java.util.List; | 9 | import java.util.List; |
| 10 | import java.util.Objects; | ||
| 11 | 10 | ||
| 12 | /** | 11 | /** |
| 13 | * @author chentao | 12 | * @author chentao |
| 14 | * @date 2025/4/23 | 13 | * @date 2025/4/23 |
| 15 | */ | 14 | */ |
| 16 | public class AtLeastOneNonNullValidator implements ConstraintValidator<AtLeastOneNonNull, Object> { | 15 | public class AtLeastOneNonNullValidator implements ConstraintValidator<AtLeastOneNonNull, CourseBo> { |
| 17 | private List<String> fields; | 16 | private List<String> fields; |
| 18 | 17 | ||
| 19 | @Override | 18 | @Override |
| ... | @@ -22,27 +21,7 @@ public class AtLeastOneNonNullValidator implements ConstraintValidator<AtLeastOn | ... | @@ -22,27 +21,7 @@ public class AtLeastOneNonNullValidator implements ConstraintValidator<AtLeastOn |
| 22 | } | 21 | } |
| 23 | 22 | ||
| 24 | @Override | 23 | @Override |
| 25 | public boolean isValid(Object object, ConstraintValidatorContext context) { | 24 | public boolean isValid(CourseBo object, ConstraintValidatorContext context) { |
| 26 | if (object == null) { | 25 | return object != null && (StringUtils.isNotBlank(object.getTwoDimensionalOssId()) || StringUtils.isNotBlank(object.getThreeDimensionalOssId())); |
| 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 | String value = String.valueOf(field.get(object)); | ||
| 35 | System.out.println(field.getName()+"============"+value+"!"); | ||
| 36 | if(StringUtils.isNotEmpty(value) && !"null".equals(value)) { | ||
| 37 | System.out.println(field.getName() + "============" + field.get(object) + "&"); | ||
| 38 | return true; // 如果至少一个字段非空,返回true | ||
| 39 | } | ||
| 40 | } | ||
| 41 | } catch (NoSuchFieldException | IllegalAccessException e) { | ||
| 42 | // 处理异常,例如记录日志等 | ||
| 43 | return false; // 出现异常时,保守起见返回false,实际中应更细致处理异常情况 | ||
| 44 | } | ||
| 45 | } | ||
| 46 | return false; // 所有指定字段均为空或出现异常,返回false | ||
| 47 | } | 26 | } |
| 48 | } | 27 | } | ... | ... |
| 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 com.lego.core.annotation.AtLeastOneNonNull; |
| 4 | import lombok.Data; | 4 | import lombok.Data; |
| 5 | 5 | ||
| 6 | import javax.validation.constraints.NotBlank; | 6 | import javax.validation.constraints.NotBlank; | ... | ... |
-
Please register or sign in to post a comment