添加校验注解
This commit is contained in:
parent
67c917562e
commit
b0e7ae05d5
@ -12,12 +12,34 @@
|
||||
|
||||
<artifactId>common-common</artifactId>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<!-- tool -->
|
||||
<slf4j-api.version>1.7.5</slf4j-api.version>
|
||||
<lombok.version>1.18.18</lombok.version>
|
||||
<spring-context.version>4.3.19.RELEASE</spring-context.version>
|
||||
<javax.validation.version>2.0.0.Final</javax.validation.version>
|
||||
<hibernate.validator.version>6.0.16.Final</hibernate.validator.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.validation</groupId>
|
||||
<artifactId>validation-api</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-validator</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
@ -30,4 +52,18 @@
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<distributionManagement>
|
||||
<!-- 两个ID必须与 setting.xml中的<server><id>Releases</id></server>保持一致 -->
|
||||
<repository>
|
||||
<id>rdc-releases</id>
|
||||
<name>Nexus Release Repository</name>
|
||||
<url>https://packages.aliyun.com/maven/repository/2005773-release-XI7cl5/</url>
|
||||
</repository>
|
||||
<snapshotRepository>
|
||||
<id>rdc-snapshots</id>
|
||||
<name>Nexus Snapshot Repository</name>
|
||||
<url>https://packages.aliyun.com/maven/repository/2005773-snapshot-V5Gjdf/</url>
|
||||
</snapshotRepository>
|
||||
</distributionManagement>
|
||||
|
||||
</project>
|
||||
|
||||
@ -0,0 +1,47 @@
|
||||
package cn.azxo.framework.common.annotation;
|
||||
|
||||
import cn.azxo.framework.common.annotation.support.DataFormatCheckValidator;
|
||||
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;
|
||||
|
||||
import javax.validation.Constraint;
|
||||
import javax.validation.Payload;
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* 日期格式校验
|
||||
*
|
||||
* @author zhaoyong_sh
|
||||
* @see DateFormatCheck
|
||||
* @since 2021-04-02 16:02
|
||||
*/
|
||||
@Target({ ElementType.FIELD })
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Constraint(validatedBy = DataFormatCheckValidator.class)
|
||||
@Documented
|
||||
public @interface DateFormatCheck {
|
||||
|
||||
String value() default "";
|
||||
|
||||
String message() default "data format is illegal";
|
||||
|
||||
Class<?>[] groups() default {};
|
||||
|
||||
Class<? extends Payload>[] payload() default {};
|
||||
|
||||
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER })
|
||||
@Retention(RUNTIME)
|
||||
@Documented
|
||||
@interface List {
|
||||
DateFormatCheck[] value();
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,55 @@
|
||||
package cn.azxo.framework.common.annotation;
|
||||
|
||||
import cn.azxo.framework.common.annotation.support.ValidValueValidator;
|
||||
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;
|
||||
|
||||
import javax.validation.Constraint;
|
||||
import javax.validation.Payload;
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* 有效的值
|
||||
*
|
||||
* @author zhaoyong_sh
|
||||
* @see ValidValue
|
||||
* @since 2020-10-10 13:56
|
||||
*/
|
||||
@Target({ FIELD})
|
||||
@Retention(RUNTIME)
|
||||
@Constraint(validatedBy = ValidValueValidator.class)
|
||||
@Documented
|
||||
public @interface ValidValue {
|
||||
|
||||
/**
|
||||
* 有效字符串集合类
|
||||
* @return
|
||||
*/
|
||||
String[] value() default {};
|
||||
|
||||
/**
|
||||
* 字符是否可以为空
|
||||
* @return
|
||||
*/
|
||||
boolean canNull() default false;
|
||||
|
||||
String message() default "invalid value";
|
||||
|
||||
Class<?>[] groups() default {};
|
||||
|
||||
Class<? extends Payload>[] payload() default {};
|
||||
|
||||
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER })
|
||||
@Retention(RUNTIME)
|
||||
@Documented
|
||||
@interface List {
|
||||
ValidValue[] value();
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,36 @@
|
||||
package cn.azxo.framework.common.annotation.support;
|
||||
|
||||
import cn.azxo.framework.common.annotation.DateFormatCheck;
|
||||
|
||||
import javax.validation.ConstraintValidator;
|
||||
import javax.validation.ConstraintValidatorContext;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
|
||||
/**
|
||||
* DateFormatCheck 校验注解实现类
|
||||
*
|
||||
* @author zhaoyong_sh
|
||||
* @see DataFormatCheckValidator
|
||||
* @since 2021-04-02 16:04
|
||||
*/
|
||||
public class DataFormatCheckValidator implements ConstraintValidator<DateFormatCheck, String> {
|
||||
|
||||
private String pattern;
|
||||
|
||||
@Override
|
||||
public void initialize(DateFormatCheck constraintAnnotation) {
|
||||
pattern = constraintAnnotation.value();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isValid(String value, ConstraintValidatorContext context) {
|
||||
SimpleDateFormat format = new SimpleDateFormat(pattern);
|
||||
try {
|
||||
format.parse(value);
|
||||
return true;
|
||||
} catch (ParseException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,49 @@
|
||||
package cn.azxo.framework.common.annotation.support;
|
||||
|
||||
import cn.azxo.framework.common.utils.ArrayUtils;
|
||||
import cn.azxo.framework.common.utils.StringUtils;
|
||||
import cn.azxo.framework.common.annotation.ValidValue;
|
||||
|
||||
import javax.validation.ConstraintValidator;
|
||||
import javax.validation.ConstraintValidatorContext;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* ValidValue 注解实现类
|
||||
*
|
||||
* @author zhaoyong_sh
|
||||
* @see ValidValueValidator
|
||||
* @since 2020-10-10 13:57
|
||||
*/
|
||||
public class ValidValueValidator implements ConstraintValidator<ValidValue, String> {
|
||||
|
||||
private String[] validValues;
|
||||
|
||||
private boolean canNull;
|
||||
|
||||
@Override
|
||||
public void initialize(ValidValue constraintAnnotation) {
|
||||
validValues = constraintAnnotation.value();
|
||||
canNull = constraintAnnotation.canNull();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isValid(String value, ConstraintValidatorContext context) {
|
||||
if(StringUtils.isBlank(value)){
|
||||
if(canNull) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if(ArrayUtils.isEmpty(validValues)){
|
||||
return false;
|
||||
}
|
||||
for (String validValue : validValues) {
|
||||
if(Objects.equals(validValue, value)){
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,43 @@
|
||||
package cn.azxo.framework.common.model;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* 通用响应对象
|
||||
*
|
||||
* @author zhaoyong_sh
|
||||
* @see CommonResponse
|
||||
* @since 2021-03-29 14:25
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class CommonResponse<T> {
|
||||
|
||||
private String code;
|
||||
|
||||
private String msg;
|
||||
|
||||
private T data;
|
||||
|
||||
public static CommonResponse success(Object data) {
|
||||
return new CommonResponse("200", "success", null);
|
||||
}
|
||||
|
||||
public static CommonResponse success(String code, String message, Object data) {
|
||||
return new CommonResponse(code, message, data);
|
||||
}
|
||||
|
||||
public static CommonResponse error(String code, String message){
|
||||
return new CommonResponse(code, message, null);
|
||||
}
|
||||
|
||||
public static CommonResponse error(String code, String message, Object data){
|
||||
return new CommonResponse(code, message, data);
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,54 @@
|
||||
package cn.azxo.framework.common.utils;
|
||||
|
||||
import java.lang.reflect.Array;
|
||||
|
||||
/**
|
||||
* 数组工具类
|
||||
*
|
||||
* @author zhaoyong_sh
|
||||
* @see ArrayUtils
|
||||
* @since 2021-04-12 15:31
|
||||
*/
|
||||
public abstract class ArrayUtils {
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
/**
|
||||
* <p>Checks if an array of Objects is empty or {@code null}.
|
||||
*
|
||||
* @param array the array to test
|
||||
* @return {@code true} if the array is empty or {@code null}
|
||||
* @since 2.1
|
||||
*/
|
||||
public static boolean isEmpty(final Object[] array) {
|
||||
return getLength(array) == 0;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
/**
|
||||
* <p>Returns the length of the specified array.
|
||||
* This method can deal with {@code Object} arrays and with primitive arrays.
|
||||
*
|
||||
* <p>If the input array is {@code null}, {@code 0} is returned.
|
||||
*
|
||||
* <pre>
|
||||
* ArrayUtils.getLength(null) = 0
|
||||
* ArrayUtils.getLength([]) = 0
|
||||
* ArrayUtils.getLength([null]) = 1
|
||||
* ArrayUtils.getLength([true, false]) = 2
|
||||
* ArrayUtils.getLength([1, 2, 3]) = 3
|
||||
* ArrayUtils.getLength(["a", "b", "c"]) = 3
|
||||
* </pre>
|
||||
*
|
||||
* @param array the array to retrieve the length from, may be null
|
||||
* @return The length of the array, or {@code 0} if the array is {@code null}
|
||||
* @throws IllegalArgumentException if the object argument is not an array.
|
||||
* @since 2.1
|
||||
*/
|
||||
public static int getLength(final Object array) {
|
||||
if (array == null) {
|
||||
return 0;
|
||||
}
|
||||
return Array.getLength(array);
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,41 @@
|
||||
package cn.azxo.framework.common.utils;
|
||||
|
||||
/**
|
||||
* 字符串工具类
|
||||
*
|
||||
* @author zhaoyong_sh
|
||||
* @see StringUtils
|
||||
* @since 2021-04-12 15:29
|
||||
*/
|
||||
public abstract class StringUtils {
|
||||
|
||||
/**
|
||||
* <p>Checks if a CharSequence is whitespace, empty ("") or null.</p>
|
||||
*
|
||||
* <pre>
|
||||
* StringUtils.isBlank(null) = true
|
||||
* StringUtils.isBlank("") = true
|
||||
* StringUtils.isBlank(" ") = true
|
||||
* StringUtils.isBlank("bob") = false
|
||||
* StringUtils.isBlank(" bob ") = false
|
||||
* </pre>
|
||||
*
|
||||
* @param cs the CharSequence to check, may be null
|
||||
* @return {@code true} if the CharSequence is null, empty or whitespace
|
||||
* @since 2.0
|
||||
* @since 3.0 Changed signature from isBlank(String) to isBlank(CharSequence)
|
||||
*/
|
||||
public static boolean isBlank(final CharSequence cs) {
|
||||
int strLen;
|
||||
if (cs == null || (strLen = cs.length()) == 0) {
|
||||
return true;
|
||||
}
|
||||
for (int i = 0; i < strLen; i++) {
|
||||
if (Character.isWhitespace(cs.charAt(i)) == false) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
37
pom.xml
37
pom.xml
@ -24,6 +24,11 @@
|
||||
<!-- tool -->
|
||||
<aspectj.version>1.7.3</aspectj.version>
|
||||
<aopalliance.version>1.0</aopalliance.version>
|
||||
<slf4j-api.version>1.7.5</slf4j-api.version>
|
||||
<lombok.version>1.18.18</lombok.version>
|
||||
<spring.framework.version>4.3.19.RELEASE</spring.framework.version>
|
||||
<javax.validation.version>2.0.0.Final</javax.validation.version>
|
||||
<hibernate.validator.version>6.0.16.Final</hibernate.validator.version>
|
||||
</properties>
|
||||
|
||||
<dependencyManagement>
|
||||
@ -44,27 +49,51 @@
|
||||
<artifactId>aopalliance</artifactId>
|
||||
<version>${aopalliance.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.validation</groupId>
|
||||
<artifactId>validation-api</artifactId>
|
||||
<version>${javax.validation.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-validator</artifactId>
|
||||
<version>${hibernate.validator.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
<version>1.7.5</version>
|
||||
<version>${slf4j-api.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-jdbc</artifactId>
|
||||
<version>4.3.19.RELEASE</version>
|
||||
<version>${spring.framework.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>4.3.19.RELEASE</version>
|
||||
<version>${spring.framework.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.18</version>
|
||||
<version>${lombok.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<distributionManagement>
|
||||
<!-- 两个ID必须与 setting.xml中的<server><id>Releases</id></server>保持一致 -->
|
||||
<repository>
|
||||
<id>rdc-releases</id>
|
||||
<name>Nexus Release Repository</name>
|
||||
<url>https://packages.aliyun.com/maven/repository/2005773-release-XI7cl5/</url>
|
||||
</repository>
|
||||
<snapshotRepository>
|
||||
<id>rdc-snapshots</id>
|
||||
<name>Nexus Snapshot Repository</name>
|
||||
<url>https://packages.aliyun.com/maven/repository/2005773-snapshot-V5Gjdf/</url>
|
||||
</snapshotRepository>
|
||||
</distributionManagement>
|
||||
|
||||
</project>
|
||||
@ -40,4 +40,18 @@
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<distributionManagement>
|
||||
<!-- 两个ID必须与 setting.xml中的<server><id>Releases</id></server>保持一致 -->
|
||||
<repository>
|
||||
<id>rdc-releases</id>
|
||||
<name>Nexus Release Repository</name>
|
||||
<url>https://packages.aliyun.com/maven/repository/2005773-release-XI7cl5/</url>
|
||||
</repository>
|
||||
<snapshotRepository>
|
||||
<id>rdc-snapshots</id>
|
||||
<name>Nexus Snapshot Repository</name>
|
||||
<url>https://packages.aliyun.com/maven/repository/2005773-snapshot-V5Gjdf/</url>
|
||||
</snapshotRepository>
|
||||
</distributionManagement>
|
||||
|
||||
</project>
|
||||
|
||||
Loading…
Reference in New Issue
Block a user