添加了重量自定义注解

grs
yjxx 2 weeks ago
parent 9ef8ec7a6b
commit cf59446afc

@ -4,6 +4,7 @@ import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.NotNull;
import jty.expressdistributionsystem.validation.inter.ValidQuality;
import lombok.Data; import lombok.Data;
@Data @Data
@ -18,7 +19,8 @@ public class GoodsDTO {
private String name; private String name;
@NotBlank(message = "快递重量不能为空") @NotBlank(message = "快递重量不能为空")
private String quality; @ValidQuality
private Double quality;
@NotBlank(message = "发货地不能为空") @NotBlank(message = "发货地不能为空")
private String addressFrom; private String addressFrom;

@ -7,6 +7,7 @@ import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.NotNull;
import jty.expressdistributionsystem.validation.inter.ValidQuality;
import lombok.Data; import lombok.Data;
import java.io.Serial; import java.io.Serial;
@ -42,5 +43,6 @@ public class Goods implements Serializable {
@TableField("quality") @TableField("quality")
@NotBlank(message = "快递重量不能为空") @NotBlank(message = "快递重量不能为空")
private String quality; @ValidQuality
private Double quality;
} }

@ -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<ValidQuality, Double> {
@Override
public boolean isValid(Double value, ConstraintValidatorContext context) {
return value != null && value > 0; // 确保数值不为null且大于0
}
}

@ -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<? extends Payload>[] payload() default {};
}
Loading…
Cancel
Save