码迷,mamicode.com
首页 > 其他好文 > 详细

service层对 @NotBlank注解起作用

时间:2020-07-01 09:52:35      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:作用   over   exce   void   ida   rod   public   null   table   

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());
            }
        }
    }

 

service层对 @NotBlank注解起作用

标签:作用   over   exce   void   ida   rod   public   null   table   

原文地址:https://www.cnblogs.com/bulrush/p/13217265.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!