Hibernate Validator快速失败模式 参数校验

文章目录

    • 依赖导入
    • 定义Validation工具类
    • 定义要校验的参数
    • 调用工具类进行参数校验

官方参考文章: https://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/#container-element-constraints

依赖导入

<dependency>
    <groupId>org.hibernate.validator</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>6.1.5.Final</version>
</dependency>

启动时可能无法启动,报如下错误:
在这里插入图片描述
原因: hibernate的版本与springboot的版本要互相兼容,一般springboot 2.+版本对应hibernate 5.+的版本

定义Validation工具类

import org.hibernate.validator.HibernateValidator;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import java.util.Set;

public class ValidationUtils {
     

    /**
     * 使用hibernate的注解来进行验证
     */
    private static final Validator VALIDATOR = Validation
            .byProvider(HibernateValidator.class)
            .configure()
            .failFast(true)  //快速失败:只要有1个参数校验不通过,就立即停止校验参数
            .buildValidatorFactory()
            .getValidator();

    /**
     * 功能描述:校验注解参数
     */
    public static <T> void validate(T obj) {
     
        Set<ConstraintViolation<T>> constraintViolations = VALIDATOR.validate(obj);
        // 抛出检验异常
        if (constraintViolations.size() > 0) {
     
            ConstraintViolation<T> constraintViolation = constraintViolations.iterator().next();
            throw new RuntimeException(String.format("参数校验失败:%s", constraintViolation.getMessage()));
        }
    }
}

定义要校验的参数

@valid :嵌套校验

public class CheckSingleFormBean {
     

    /**
     * 发病时间
     */
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date onset;
    /**
     * 症状
     */
    @NotEmpty(message = "请填写正确主诉信息")
    @Size(max = 20, message = "主诉不能超过{max}个")
    private List<@NotNull(message = "请填写正确主诉信息") CheckSingleSymptomEntity> symptoms;

    /**
     * 中医诊断结果
     */
    @Size(max = 20, message = "诊断个数不能超过{max}个")
    private List<@NotNull(message = "中医诊断编号不能为null") Long> tcmDiseaseIds;

    /**
     * 西医诊断结果
     */
    //@NotEmpty(message = "请填写正确西医诊断信息")
    @Size(max = 20, message = "诊断个数不能超过{max}个")
    private List<@NotNull(message = "西医疾病编号不能为空") Long> wmDiseaseIds;

    /**
     * 自定义诊断结果
     */
    @Size(max = 20, message = "诊断个数不能超过{max}个")
    private List<@NotNull(message = "自定义疾病编号不能为空") Long> ownDiseaseIds;
    /**
     * 体格检查
     */
    @Length(max = 500, message = "请填写正确体格检查")
    private String physicalExamination;
    /**
     * 治疗意见
     */
    @Length(max = 500, message = "请填写正确治疗意见信息")
    private String treatSuggestion;

    /**
     * 医嘱
     */
    @Length(max = 500, message = "请填写正确医嘱信息")
    private String doctorAdvice;

    @NotNull(message = "患者信息不能为空")
    @Valid
    private SavePatientFormbean patientFormBean;

    @Valid
    private SavePrescriptionTcmFormbean savePrescriptionTcmFormbean;

    @Valid
    private SavePrescriptionWmFormbean savePrescriptionWmFormbean;

    @Valid
    private SaveTreatmentFormBean saveTreatmentFormBean;
}

调用工具类进行参数校验

ValidationUtils.validate(requestDto);

你可能感兴趣的:(开发工具,JavaWeb)