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 {}; +}