1、validation的使用
1、配置一个注解用来只在几个值之间选择
1、创建注解EnumValue
package com.asto.tax.common.validation.annotation;
/**
* @author djy
* @createTime 2021/11/24 下午4:47
* @description 用于检验传递的参数必须是在指定的值中,但是也可以通过require来指定 这个值可以为空的情况
* * 使用案例
* * @EnumValue(strValues = {"ALL", "REVIEWED", "PROCESSING", "WAIT_INVOICE", "INVOICED"},
* * message = "如果传了值,必须是ALL,REVIEWED,PROCESSING,WAIT_INVOICE,INVOICED",require = false)
* * private String invoiceStatus;
*/
import com.asto.tax.common.validation.annotation.validator.EnumValueValidator;
import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
@Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER})
@Retention(RUNTIME)
@Documented
@Constraint(validatedBy = {EnumValueValidator.class})
public @interface EnumValue {
// 默认错误消息
String message() default "必须为指定值";
String[] strValues() default {};
int[] intValues() default {};
// 分组
Class<?>[] groups() default {};
/**
* 是否是必须有值 默认是
*/
boolean require() default true;
// 负载
Class<? extends Payload>[] payload() default {};
// 指定多个时使用
@Target({FIELD, METHOD, PARAMETER, ANNOTATION_TYPE})
@Retention(RUNTIME)
@Documented
@interface List {
EnumValue[] value();
}
}
2、创建注解校验器EnumValueValidator
- 主要是在方法isValid中进行判断 ```java package com.asto.tax.common.validation.annotation.validator;
import com.asto.tax.common.validation.annotation.EnumValue;
import javax.validation.ConstraintValidator; import javax.validation.ConstraintValidatorContext;
/**
- @author djy
- @createTime 2021/11/24 下午4:49
@description */ public class EnumValueValidator implements ConstraintValidator
{ private String[] strValues; private int[] intValues; private boolean require; @Override public void initialize(EnumValue constraintAnnotation) {
strValues = constraintAnnotation.strValues();
intValues = constraintAnnotation.intValues();
require = constraintAnnotation.require();
}
@Override public boolean isValid(Object value, ConstraintValidatorContext context) {
if (value instanceof String) { for (String s : strValues) { if (s.equals(value)) { return true; } } return false; } else if (value instanceof Integer) { for (int s : intValues) { if (s == ((Integer) value).intValue()) { return true; } } return false; } return !this.require;
} }
<a name="dLRPw"></a>
### 3、使用示例
```java
package com.asto.tax.supervise.pojo.dto;
import com.asto.tax.common.validation.annotation.EnumValue;
import lombok.Data;
import lombok.experimental.Accessors;
import java.io.Serializable;
/**
* @author djy
* @createTime 2021/11/25 上午11:27
* @description 用于交税管理-增值税列表查询
*/
@Data
@Accessors(chain = true)
public class OrderInvoiceAddTaxSearchDto extends SearchPageListDto implements Serializable {
private static final long serialVersionUID = -5707180282202808724L;
@EnumValue(strValues = {"ALL", "REVIEWED", "PROCESSING", "WAIT_INVOICE", "INVOICED"},
message = "如果传了值,必须是ALL,REVIEWED,PROCESSING,WAIT_INVOICE,INVOICED", require = false)
private String invoiceStatus;
}
4、总结
- 可以达到如果是上面这些值就会通过校验。
2、自定义配置正则注解在字段是null和空字符串的时候都直接放行
- 默认validation中@Pattern注解是只对值是null的时候是放行的。
1、自定义创建一个注解@PatternNullable
```java /*- Bean Validation API *
- License: Apache License, Version 2.0
- See the license.txt file in the root directory or http://www.apache.org/licenses/LICENSE-2.0. */ package com.asto.tax.common.validation.annotation;
import com.asto.tax.common.validation.annotation.validator.PatternNullableValidator;
import javax.validation.Constraint; import javax.validation.Payload; import javax.validation.constraints.Pattern.List; import java.lang.annotation.Documented; import java.lang.annotation.Repeatable; import java.lang.annotation.Retention; import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.*; import static java.lang.annotation.RetentionPolicy.RUNTIME;
/**
- The annotated {@code CharSequence} must match the specified regular expression.
- The regular expression follows the Java regular expression conventions
- see {@link java.util.regex.Pattern}.
- Accepts {@code CharSequence}. {@code null} elements are considered valid. *
@author djy */ @Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE }) @Retention(RUNTIME) @Documented @Constraint(validatedBy = {PatternNullableValidator.class}) public @interface PatternNullable {
/**
@return the regular expression to match */ String regexp();
/**
@return the error message template */ String message() default “{javax.validation.constraints.Pattern.message}”;
/**
@return the groups the constraint belongs to */ Class<?>[] groups() default { };
/**
- @return the payload associated to the constraint */ Class<? extends Payload>[] payload() default { };
}
<a name="YBQPh"></a>
### 2、添加验证器PatternNullableValidator
- 解析注解用来验证那种类型的值,并且什么情况下进行放行。
```java
/*
* Hibernate Validator, declare and validate application constraints
*
* License: Apache License, Version 2.0
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
*/
package com.asto.tax.common.validation.annotation.validator;
import com.asto.tax.common.validation.annotation.PatternNullable;
import org.hibernate.validator.constraintvalidation.HibernateConstraintValidatorContext;
import org.hibernate.validator.internal.engine.messageinterpolation.util.InterpolationHelper;
import org.hibernate.validator.internal.util.logging.Log;
import org.hibernate.validator.internal.util.logging.LoggerFactory;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.lang.invoke.MethodHandles;
import java.util.regex.Matcher;
import java.util.regex.PatternSyntaxException;
/**
* 如果是空值的就不验证了直接放行 有值才验证
* @author djy
*/
public class PatternNullableValidator implements ConstraintValidator<PatternNullable, CharSequence> {
private static final Log LOG = LoggerFactory.make(MethodHandles.lookup());
private java.util.regex.Pattern pattern;
private String escapedRegexp;
@Override
public void initialize(PatternNullable parameters) {
try {
pattern = java.util.regex.Pattern.compile(parameters.regexp());
} catch (PatternSyntaxException e) {
throw LOG.getInvalidRegularExpressionException(e);
}
escapedRegexp = InterpolationHelper.escapeMessageParameter(parameters.regexp());
}
@Override
public boolean isValid(CharSequence value, ConstraintValidatorContext constraintValidatorContext) {
if (value == null || value.length() == 0) {
//如果是空值直接放行 不验证了
return true;
}
if (constraintValidatorContext instanceof HibernateConstraintValidatorContext) {
constraintValidatorContext.unwrap(HibernateConstraintValidatorContext.class).addMessageParameter("regexp", escapedRegexp);
}
Matcher m = pattern.matcher(value);
return m.matches();
}
}