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