自定义枚举检验
- Maven依赖
springboot2.3之后,hibernate-validator依赖缺失,需要添加spring-boot-starter-validation依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
- 自定义枚举注解@EnumValue
package com.example.common.validation;
import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
import static java.lang.annotation.ElementType.CONSTRUCTOR;
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;
/**
* ClassName: EnumValue
* Description: 枚举校验注解
*/
@Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Constraint(validatedBy = {EnumValueValidator.class})
public @interface EnumValue {
String message() default "type not found";
String[] strValues() default {};
int[] intValues() default {};
byte[] byteValues() default {};
Class<? extends Enum<?>>[] enumValues() default {};
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
@Target({FIELD, METHOD, PARAMETER, ANNOTATION_TYPE})
@Retention(RUNTIME)
@Documented
@interface List {
EnumValue[] value();
}
}
- 创建EnumValueValidator类,实现ConstraintValidator接口
package com.example.common.validation;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
/**
* ClassName: EnumValueValidator
* Description: 枚举校验处理类
*/
public class EnumValueValidator implements ConstraintValidator<EnumValue, Object> {
private String[] strValues;
private int[] intValues;
private byte[] byteValues;
private Class<? extends Enum<?>>[] enumValues;
/**
* 获取注解配置参数
* @param constraintAnnotation 注解参数
*/
@Override
public void initialize(EnumValue constraintAnnotation) {
this.strValues = constraintAnnotation.strValues();
this.intValues = constraintAnnotation.intValues();
this.byteValues = constraintAnnotation.byteValues();
this.enumValues = constraintAnnotation.enumValues();
}
/**
* 校验入参是否满足配置值
*/
@Override
public boolean isValid(Object val, ConstraintValidatorContext constraintValidatorContext) {
if (val == null) {
return true;
}
return validLogic(val);
}
/**
* 校验逻辑
*/
private boolean validLogic(Object val) {
if (val instanceof String) {
String strVal = (String) val;
if (strVal.length() == 0) {
return true;
}
if (isArraysNotEmpty(strValues)) {
for (String sv : strValues) {
if (sv.equals(val)) {
return true;
}
}
}
if (isArraysNotEmpty(enumValues)) {
for (Class<? extends Enum<?>> evc : enumValues) {
Enum<?>[] enums = evc.getEnumConstants();
for (Enum<?> enu : enums) {
if (enu.name().equals(val)) {
return true;
}
}
}
}
} else if (val instanceof Integer) {
for (int iv : intValues) {
if (iv == (Integer) val) {
return true;
}
}
} else if (val instanceof Byte) {
for (byte bv : byteValues) {
if (bv == (Byte) val) {
return true;
}
}
}
return false;
}
private boolean isArraysNotEmpty(Object[] objs) {
return objs != null && objs.length > 0;
}
}
- 使用
枚举类
package com.example.common.enums;
/**
* ClassName: DateType
* Description:
*/
public enum DateType {
DAY("天"),
WEEK("周"),
MONTH("月"),
;
private final String desc;
DateType(String desc) {
this.desc = desc;
}
public String getDesc() {
return desc;
}
}
参数类ValidatedTest
package com.example.model.param;
import com.example.common.enums.DateType;
import com.example.common.validation.EnumValue;
/**
* ClassName: ValidatedTest
* Description:
*/
public class ValidatedTest {
@EnumValue(enumValues = DateType.class)
private String dateType;
public String getDateType() {
return dateType;
}
public void setDateType(String dateType) {
this.dateType = dateType;
}
}
Controller
package com.example.controller;
import cn.hutool.json.JSONUtil;
import com.example.model.param.ValidatedTest;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* ClassName: ValidatedTestController
* Description:
*/
@RestController
@RequestMapping("/validated")
public class ValidatedTestController {
@PostMapping("/test")
public String test1(@RequestBody @Validated ValidatedTest vt) {
System.out.println(JSONUtil.toJsonPrettyStr(vt));
return "SUCCESS: " + vt.getDateType();
}
}
版权声明:本文为w_shi_yi原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。