service层对 @NotBlank注解起作用


1:在实体类上加上注解

/**
     * 产品名称
     */
    @TableField("product_name")
    @NotBlank
    private String productName;

2:service的校验方法

@Override
    public void validationOrderRelationshipNotice(OrderRelationshipNotice orderRelationshipNotice) throws ValidationException{
        //校验参数是否为空
        Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
        Set<ConstraintViolation<OrderRelationshipNotice>> constraintViolations = validator.validate(orderRelationshipNotice);
        List<ConstraintViolation<OrderRelationshipNotice>> list = new ArrayList(constraintViolations);
        if (!CollectionUtils.isEmpty(list)) {
            ConstraintViolation<OrderRelationshipNotice> constraintViolation = list.get(0);
            if (constraintViolation != null) {
                throw new ValidationException(constraintViolation.getMessage());
            }
        }
    }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM