From cf59446afca6b8ab6d1d746539f191b01e0f2ab1 Mon Sep 17 00:00:00 2001 From: yjxx <1055683039@qq.com> Date: Tue, 12 Nov 2024 13:06:24 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86=E9=87=8D=E9=87=8F?= =?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89=E6=B3=A8=E8=A7=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DTO/GoodsDTO.java | 4 +++- .../entity/Goods.java | 4 +++- .../validation/QualityValidator.java | 13 ++++++++++++ .../validation/inter/ValidQuality.java | 21 +++++++++++++++++++ 4 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 src/main/java/jty/expressdistributionsystem/validation/QualityValidator.java create mode 100644 src/main/java/jty/expressdistributionsystem/validation/inter/ValidQuality.java diff --git a/src/main/java/jty/expressdistributionsystem/DTO/GoodsDTO.java b/src/main/java/jty/expressdistributionsystem/DTO/GoodsDTO.java index f33fb46..69fb69b 100644 --- a/src/main/java/jty/expressdistributionsystem/DTO/GoodsDTO.java +++ b/src/main/java/jty/expressdistributionsystem/DTO/GoodsDTO.java @@ -4,6 +4,7 @@ import com.fasterxml.jackson.databind.annotation.JsonSerialize; import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotNull; +import jty.expressdistributionsystem.validation.inter.ValidQuality; import lombok.Data; @Data @@ -18,7 +19,8 @@ public class GoodsDTO { private String name; @NotBlank(message = "快递重量不能为空") - private String quality; + @ValidQuality + private Double quality; @NotBlank(message = "发货地不能为空") private String addressFrom; diff --git a/src/main/java/jty/expressdistributionsystem/entity/Goods.java b/src/main/java/jty/expressdistributionsystem/entity/Goods.java index 512bda3..310e125 100644 --- a/src/main/java/jty/expressdistributionsystem/entity/Goods.java +++ b/src/main/java/jty/expressdistributionsystem/entity/Goods.java @@ -7,6 +7,7 @@ import com.fasterxml.jackson.databind.annotation.JsonSerialize; import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotNull; +import jty.expressdistributionsystem.validation.inter.ValidQuality; import lombok.Data; import java.io.Serial; @@ -42,5 +43,6 @@ public class Goods implements Serializable { @TableField("quality") @NotBlank(message = "快递重量不能为空") - private String quality; + @ValidQuality + private Double quality; } diff --git a/src/main/java/jty/expressdistributionsystem/validation/QualityValidator.java b/src/main/java/jty/expressdistributionsystem/validation/QualityValidator.java new file mode 100644 index 0000000..3cbcd38 --- /dev/null +++ b/src/main/java/jty/expressdistributionsystem/validation/QualityValidator.java @@ -0,0 +1,13 @@ +package jty.expressdistributionsystem.validation; + +import jakarta.validation.ConstraintValidator; +import jakarta.validation.ConstraintValidatorContext; +import jty.expressdistributionsystem.validation.inter.ValidQuality; + +public class QualityValidator implements ConstraintValidator { + + @Override + public boolean isValid(Double value, ConstraintValidatorContext context) { + return value != null && value > 0; // 确保数值不为null且大于0 + } +} diff --git a/src/main/java/jty/expressdistributionsystem/validation/inter/ValidQuality.java b/src/main/java/jty/expressdistributionsystem/validation/inter/ValidQuality.java new file mode 100644 index 0000000..a462101 --- /dev/null +++ b/src/main/java/jty/expressdistributionsystem/validation/inter/ValidQuality.java @@ -0,0 +1,21 @@ +package jty.expressdistributionsystem.validation.inter; + +import jakarta.validation.Constraint; +import jakarta.validation.Payload; +import jty.expressdistributionsystem.validation.QualityValidator; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Constraint(validatedBy = QualityValidator.class) // 指定验证器 +@Target({ ElementType.FIELD, ElementType.METHOD, ElementType.ANNOTATION_TYPE, ElementType.PARAMETER }) +@Retention(RetentionPolicy.RUNTIME) +public @interface ValidQuality { + String message() default "快递重量必须大于0"; + + Class[] groups() default {}; + + Class[] payload() default {}; +}