Hibernate Validator入门及使用

前言

最近在开发某个模块的功能时,里面包含大量对请求参数判空和验证的步骤且参数繁多,存在大量的重复判断,验证影响代码的美观遂决定优化这部分代码。SpringBoot自带了参数验证框Hibernate Validator,下面是自己的学习笔记。

依赖

若使用SptingBoot,在spring-boot-starter-web包中包含此框架依赖,其他框架加入依赖即可
<dependency>
      <groupId>org.hibernate.validator</groupId>
      <artifactId>hibernate-validator</artifactId>
      <version>6.0.17.Final</version>
    </dependency>

使用

新建类User

package com.longhc.uublog;

import com.alibaba.fastjson.JSON;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import java.io.Serializable;

@Getter
@Setter
@NoArgsConstructor
public class User implements Serializable {
    private static final long serialVersionUID = -2164567260938543876L;

    private String userId;
    
    // 字段userName不可为null,且length>0
    @NotBlank
    private String userName;
    
    // 密码需要匹配正则表达式
    @Pattern(regexp = "^[A-Z]{0,3}[0-9]{5,10}")
    private String password;

    // 验证是否是正确的电子邮件格式
    @Email
    private String email;

    // 家庭住址不为空
    @NotNull
    private String address;

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }
}
  • 使用Validator校验传入参数是否符合规则
package com.longhc.uublog;

import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import java.util.Set;

@Slf4j
public class VaildatorTest {

    @Test
    public void test() {
        // 获得默认的Validator
        Validator validator = Validation.buildDefaultValidatorFactory().getValidator();

        Consumer consumer = new Consumer();
        consumer.setUserName("longhaicheng");
        consumer.setAddress(null);
        consumer.setEmail("123456789");
        // 验证consumer是否符合要求,并返回set集合,若set集合size()返回>0,说明存在违规的参数,通过日志打印
        Set<ConstraintViolation<Consumer>> constraintViolations = validator.validate(consumer);
        if (constraintViolations.size() > 0) {
            for (ConstraintViolation<Consumer> constraintViolation : constraintViolations) {
                String message = constraintViolation.getMessage();
                log.error("message:{}", message);
            }
        }
    }
}
  • 日志返回:

Hibernate Validator入门及使用

  • 参数校验失败返回错误消息,也可以定制错误消息,通过注解中的message属性配置
@NotBlank(message = "字段[userName]不可为空")
@Pattern(regexp = "^[A-Z]{0,3}[0-9]{5,10}",message = "字段[password]需匹配表达式[{regexp}]")
@Email(message = "错误的电子邮箱格式")
@NotNull(message = "家庭住址不为空")
  • 要在Spring中使用此框架
package com.longhc.uublog;

import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.HibernateValidator;
import org.springframework.stereotype.Component;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import java.util.Set;



@Component
@Slf4j
public class ValidatorUtil {

    public static Validator getValidator() {
        // 使用HibernateValidator
        Validator validator = Validation.byProvider(HibernateValidator.class)
                .configure()
                // 快速失败(即:第一个参数校验失败就返回错误信息,而不是校验所有的参数,并一次性返回所有的错误信息)
                .failFast(true)
                .buildValidatorFactory()
                .getValidator();
        return validator;
    }

    /**
     * @param object object
     * @param groups groups
     */
    public void validateObject(Object object, Class<?>... groups) {
        Set<ConstraintViolation<Object>> constraintViolations = getValidator().validate(object, groups);
        if (constraintViolations.size() > 0) {
            for (ConstraintViolation<Object> constraintViolation : constraintViolations) {
                String message = constraintViolation.getMessage();
                log.error("errorMessage:{}", message);
            }
        }
    }
}