新增订单模块
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
package com.starry.common.annotation;
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
* 枚举参数校验注解
|
||||
*
|
||||
* @author admin
|
||||
*/
|
||||
@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 {};
|
||||
|
||||
// 负载
|
||||
Class<? extends Payload>[] payload() default {};
|
||||
|
||||
// 指定多个时使用
|
||||
@Target({FIELD, METHOD, PARAMETER, ANNOTATION_TYPE})
|
||||
@Retention(RUNTIME)
|
||||
@Documented
|
||||
@interface List {
|
||||
EnumValue[] value();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.starry.common.annotation;
|
||||
|
||||
import javax.validation.ConstraintValidator;
|
||||
import javax.validation.ConstraintValidatorContext;
|
||||
|
||||
/**
|
||||
* 枚举校验注解处理类
|
||||
*/
|
||||
public class EnumValueValidator implements ConstraintValidator<EnumValue, Object> {
|
||||
|
||||
private String[] strValues;
|
||||
private int[] intValues;
|
||||
|
||||
@Override
|
||||
public void initialize(EnumValue constraintAnnotation) {
|
||||
strValues = constraintAnnotation.strValues();
|
||||
intValues = constraintAnnotation.intValues();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isValid(Object value, ConstraintValidatorContext context) {
|
||||
if (value instanceof String) {
|
||||
for (String s : strValues) {
|
||||
if (s.equals(value)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
} else if (value instanceof Integer) {
|
||||
for (int s : intValues) {
|
||||
if (s == (Integer) value) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user