参数校验

@RequestMapping(value = /test,method = RequestMethod.POST)
public void test(@RequestBody @Valid SyncProductInfoForm syncProductInfoForm,
BindingResult bindingResult)//将校验结果放到BindingResult对象中 {
	logger.info("syncProductinfo param={}",syncProductInfoForm);
	if(bindingResult.hasErrors()) {
		ObjectError objectError = bindingResult.getAllErrors().get(0);
		Response.buildResponseWithMsg(result, ResponseEnum.PARAM_ERROR.getCode(), objectError.getDefaultMessage());
		return result;
	}
}


import lombok.Data;
import org.hibernate.validator.constraints.NotBlank;
import org.hibernate.validator.constraints.NotEmpty;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

@Data
public class SyncProductInfoForm {
    /*cms商品id*/
    @NotBlank(message = "cmsProductId不能为空")
    private String cmsProductId;
    /*扩展图片*/
    @Valid
    private List<ProductExtendImgForm> productExtendImgList;
    /*sku信息*/
    @Valid //用来递归校验-多层校验
    @NotEmpty(message = "productSkuInfoList不能为空")
    private List<ProductSkuInfoForm> productSkuInfoList;
}

@Data
public class ProductExtendImgForm {
    /*图片地址*/
    @NotBlank(message = "productImgUrl不能为空")
    private String productImgUrl;
    /*图片权重*/
    private String productImgWeight;
}
复制代码

校验方式二:

    public static void main(String[] args) {
        PostBackBindCarInfoForm postBackBindCarInfoForm = new PostBackBindCarInfoForm();
        postBackBindCarInfoForm.setCarNo("");
        ValidatorFactory vf = Validation.buildDefaultValidatorFactory();
        Validator validator = vf.getValidator();
        Set<ConstraintViolation<PostBackBindCarInfoForm>> set = validator.validate(postBackBindCarInfoForm);
        set.stream().forEach(item->{
            System.out.println(item.getMessage());
        });
    }
复制代码

参考:www.cnblogs.com/cjsblog/p/8…

转载于:https://juejin.im/post/5d19b282f265da1bbb03f293

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值