2023年11月29日发(作者:)

java⾃定义注解验证实体类

1.创建⼀个⾃定义注解

/**

* 实体检验⾃定义注解类

* @author

*/

@Target()

@Retention(E)

public @interface ValidateEntity {

public boolean required() default false;//是否检验null

public boolean requiredLeng() default false;//是否检验长度

public int maxLength() default -1;//最⼤长度

public int minLength() default -1;//最⼩长度

public String errorRequiredMsg() default "";//值为null时的错误提⽰信息

public String errorMinLengthMsg() default "";//最⼩长度不满⾜时的提⽰信息

public String errorMaxLengthMsg() default "";//最⼤长度不满⾜时的提⽰信息

}

/**

* 验证实体⼯具类

* @author

*/

public class ValidateEntityUtil {

public static JsonResult validate(Object object) {

Field[] declaredFields = ss().getDeclaredFields();

for (Field field : declaredFields) {

ValidateEntity annotation = otation();

if (annotation != null) {

if (ed()) {

//表⽰该字段是必填字段

essible(true);

try {

Object o = (object);

//⾸先判断是否为空

if (y(o)) {

4.编写接⼝测试

@RequestMapping("test")

JsonResult test(Problem problem){