All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.hugh.aop.constraints.validator.NotEmptyValidator Maven / Gradle / Ivy

The newest version!
package com.github.hugh.aop.constraints.validator;

import com.github.hugh.aop.constraints.NotEmpty;
import com.github.hugh.util.EmptyUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * 不为空注解校验实现类
 *
 * @author hugh
 * @since 1.0.5
 */
public class NotEmptyValidator implements ConstraintValidator {

    private String message;
    @Override
    public void initialize(NotEmpty notEmpty) {
        this.message = notEmpty.value(); // 获取注解的 value 属性值
    }

    /**
     * 校验value是否不为空、
     * 

{@code false} 则抛出校验异常

* * @param value 参数 * @param constraintValidatorContext 源 * @return boolean {@code true}不为空 */ @Override public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) { // 如果值为null或空字符串,则禁用默认约束违规提示,并使用指定消息构建自定义约束违规提示 if (value == null || value.length() == 0) { constraintValidatorContext.disableDefaultConstraintViolation(); // 禁用默认约束违规提示 constraintValidatorContext.buildConstraintViolationWithTemplate(message).addConstraintViolation(); // 使用指定消息构建自定义约束违规提示 } return EmptyUtils.isNotEmpty(value); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy