不说了,上代码
定义注解
package com.xxx.common.validate;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Documented;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import javax.validation.Constraint;
import javax.validation.Payload;
import com.xxx.common.validate.In.List;
import com.xxx.common.validate.validator.InValidator;
@Documented
@Constraint(validatedBy = {InValidator.class})
@Target({ METHOD, FIELD, PARAMETER })
@Retention(RUNTIME)
@Repeatable(List.class)
public @interface In {
String message();//所有validation注解必须的方法
Class<?>[] groups() default { };//所有validation注解必须的方法
Class<? extends Payload>[] payload() default { };//所有validation注解必须的方法
String[] value();
@Target({ METHOD, FIELD, PARAMETER })
@Retention(RUNTIME)
@Documented
public @interface List {
In[] value();
}
}
定义注解处理类
package com.xxx.common.validate.validator;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.List;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import org.apache.commons.lang3.StringUtils;
import com.xxx.common.dto.ServiceException;
import com.xxx.common.validate.In;
import lombok.extern.slf4j.Slf4j;
@Slf4j//lombok注解
public class InValidator implements ConstraintValidator<In, Comparable<?>> {
private List<String> list;
@Override
public void initialize(In constraintAnnotation) {
String[] values = constraintAnnotation.value();
for (String value : values) {
if (StringUtils.isBlank(value)) {
//一个自定义的运行时异常类
throw new ServiceException("@In中任何元素不能包含blank值");
}
}
this.list = Arrays.asList(values);
}
@Override
public boolean isValid(Comparable<?> value, ConstraintValidatorContext context) {
if (value == null) {
return true;
}
List<String> list = this.list;
if (list.isEmpty()) {
return false;
}
try {
//数值类型的值用compareTo比较可以避免一些格式上的bug
if (value instanceof BigDecimal) {
BigDecimal bigDecimal = (BigDecimal) value;
for (String el : list) {
if (bigDecimal.compareTo(new BigDecimal(el)) == 0) {
return true;
}
}
} else if (value instanceof BigInteger) {
BigInteger bigInteger = (BigInteger) value;
for (String el : list) {
if (bigInteger.compareTo(new BigInteger(el)) == 0) {
return true;
}
}
} else if (value instanceof Integer) {
Integer integer = (Integer) value;
for (String el : list) {
if (integer.compareTo(Integer.valueOf(el)) == 0) {
return true;
}
}
} else if (value instanceof Long) {
Long longer = (Long) value;
for (String el : list) {
if (longer.compareTo(Long.valueOf(el)) == 0) {
return true;
}
}
} else if (value instanceof Short) {
Short shorter = (Short) value;
for (String el : list) {
if (shorter.compareTo(Short.valueOf(el)) == 0) {
return true;
}
}
} else if (value instanceof Byte) {
Byte byter = (Byte) value;
for (String el : list) {
if (byter.compareTo(Byte.valueOf(el)) == 0) {
return true;
}
}
} else {
if (list.contains(value.toString())) {
return true;
}
}
} catch (NumberFormatException e) {
log.error(list.toString() + "元素转数字异常", e);
}
return false;
}
}
用法
/**
* groups 根据需要可省略或修改,Save.class是自定义的一个空接口,用来标识分组
* value 必填,字符串数组
* message 必填,验证不通过时的提示信息,{value}是spring的spel表达式,表示引用value的值
* 可标注在BigDecimal、BigInteger、Integer、Long、Short、Byte、Charactor、String及其原始类型上,它们都是Comparable类型;也可以标注在其它Comparable类型上,验证时调用其toString方法比较
*/
@In(groups = {Save.class}, value = {"00", "01", "02"}, message = "值必须是{value}之一")
结语
如有错误或不足之处,欢迎指正