商品添加校验完善
This commit is contained in:
parent
ac936caf17
commit
af5c0a4641
@ -0,0 +1,44 @@
|
||||
package cn.lili.common.validation;
|
||||
|
||||
import cn.lili.common.validation.impl.EnumValuesValidator;
|
||||
|
||||
import javax.validation.Constraint;
|
||||
import javax.validation.Payload;
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import static java.lang.annotation.ElementType.*;
|
||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||
|
||||
/**
|
||||
* 枚举值校验注解
|
||||
*
|
||||
* @author Bulbasaur
|
||||
*/
|
||||
@Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER})
|
||||
@Retention(RUNTIME)
|
||||
@Documented
|
||||
@Constraint(validatedBy = {EnumValuesValidator.class})
|
||||
public @interface EnumValue {
|
||||
|
||||
String message() default "必须为指定值";
|
||||
|
||||
String[] strValues() default {};
|
||||
|
||||
int[] intValues() default {};
|
||||
|
||||
//分组
|
||||
Class<?>[] groups() default {};
|
||||
|
||||
//负载
|
||||
Class<? extends Payload>[] payload() default {};
|
||||
|
||||
|
||||
@Target({FIELD, METHOD, PARAMETER, ANNOTATION_TYPE})
|
||||
@Retention(RUNTIME)
|
||||
@Documented
|
||||
@interface List {
|
||||
EnumValue[] value();
|
||||
}
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
package cn.lili.common.validation.impl;
|
||||
|
||||
import cn.lili.common.validation.EnumValue;
|
||||
|
||||
import javax.validation.ConstraintValidator;
|
||||
import javax.validation.ConstraintValidatorContext;
|
||||
|
||||
/**
|
||||
* 枚举之校验
|
||||
*
|
||||
* @author Bulbasaur
|
||||
* @since 2021/7/9 1:41 上午
|
||||
*/
|
||||
public class EnumValuesValidator implements ConstraintValidator<EnumValue, Object> {
|
||||
|
||||
private String[] strValues;
|
||||
private int[] intValues;
|
||||
|
||||
@Override
|
||||
public boolean isValid(Object o, ConstraintValidatorContext constraintValidatorContext) {
|
||||
if (o instanceof String) {
|
||||
for (String s : strValues) {
|
||||
if (s.equals(o)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
} else if (o instanceof Integer) {
|
||||
for (int s : intValues) {
|
||||
if (s == ((Integer) o).intValue()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initialize(EnumValue constraintAnnotation) {
|
||||
strValues = constraintAnnotation.strValues();
|
||||
intValues = constraintAnnotation.intValues();
|
||||
}
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
package cn.lili.modules.goods.entity.dto;
|
||||
|
||||
import cn.lili.common.validation.EnumValue;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
import lombok.ToString;
|
||||
@ -43,6 +44,7 @@ public class GoodsOperationDTO implements Serializable {
|
||||
|
||||
@ApiModelProperty(value = "商品名称", required = true)
|
||||
@NotEmpty(message = "商品名称不能为空")
|
||||
@Length(max = 50, message = "商品名称不能超过50个字符")
|
||||
private String goodsName;
|
||||
|
||||
@ApiModelProperty(value = "商品编号", required = true)
|
||||
@ -57,12 +59,14 @@ public class GoodsOperationDTO implements Serializable {
|
||||
|
||||
@ApiModelProperty(value = "市场价格", required = true)
|
||||
@NotNull(message = "市场价格不能为空")
|
||||
@Min(value = 0, message = "市场价格不能为负数")
|
||||
@Max(value = 99999999, message = "市场价格不能超过99999999")
|
||||
private Double cost;
|
||||
|
||||
@ApiModelProperty(value = "重量", required = true)
|
||||
@NotNull(message = "商品重量不能为空")
|
||||
@Min(value = 0, message = "重量不能为负数")
|
||||
@Max(value = 99999999, message = "重量不能超过99999999")
|
||||
@Min(value = 0, message = "商品重量不能为负数")
|
||||
@Max(value = 99999999, message = "商品重量不能超过99999999")
|
||||
private Double weight;
|
||||
|
||||
@ApiModelProperty(value = "详情")
|
||||
@ -72,6 +76,7 @@ public class GoodsOperationDTO implements Serializable {
|
||||
private String mobileIntro;
|
||||
|
||||
@ApiModelProperty(value = "库存")
|
||||
@Min(value = 0, message = "库存不能为负数")
|
||||
@Max(value = 99999999, message = "库存不能超过99999999")
|
||||
private Integer quantity;
|
||||
|
||||
@ -118,6 +123,7 @@ public class GoodsOperationDTO implements Serializable {
|
||||
* @see cn.lili.modules.goods.entity.enums.GoodsTypeEnum
|
||||
*/
|
||||
@ApiModelProperty(value = "商品类型")
|
||||
@EnumValue(strValues = {"PHYSICAL_GOODS","VIRTUAL_GOODS","E_COUPON"},message = "商品类型参数值错误")
|
||||
private String goodsType;
|
||||
|
||||
/**
|
||||
|
@ -26,6 +26,7 @@ import io.swagger.annotations.ApiOperation;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
@ -100,7 +101,7 @@ public class GoodsStoreController {
|
||||
|
||||
@ApiOperation(value = "新增商品")
|
||||
@PostMapping(value = "/create", consumes = "application/json", produces = "application/json")
|
||||
public ResultMessage<GoodsOperationDTO> save(@RequestBody GoodsOperationDTO goodsOperationDTO) {
|
||||
public ResultMessage<GoodsOperationDTO> save(@Valid @RequestBody GoodsOperationDTO goodsOperationDTO) {
|
||||
goodsService.addGoods(goodsOperationDTO);
|
||||
return ResultUtil.success();
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user