validation自定義註解校驗列舉型別

2021-09-12 02:19:36 字數 1308 閱讀 3627

一、定義乙個校驗註解,類似於@notnull @size等等那樣

/**

* 列舉校驗註解

* * @author ye17186

* @version 2019/3/6 15:53

*/@target()

@retention(retentionpolicy.runtime)

@documented

@constraint(validatedby = )

public @inte***ce enumvalid ;

class<? extends payload> payload() default {};

/*** 目標列舉類

*/class<?> target() default class.class;

/*** 是否忽略空值

*/boolean ignoreempty() default true;

}

二、自定義列舉校驗的處理類,該類必須實現constraintvalidator介面

/**

* 列舉引數校驗處理類

* * @author ye17186

* @version 2019/3/6 15:56

*/@slf4j

public class enumvalidator implements constraintvalidator

@override

public boolean isvalid(string value, constraintvalidatorcontext constraintvalidatorcontext)

}} catch (nosuchmethodexception | illegalacces***ception | invocationtargetexception e)

} else

return result;

}}

三、**中使用

1、定義乙個列舉類

public enum testenum
2、引數校驗該列舉,注意接收引數型別必須為string,否則需要進行特殊處理才行

public class testrequest implements serializable
3、controller中直接加上@validated 或 @valid即可

public apiresp dosomething(@validated @requestbody testrequest request)

自定義註解

target elementtype.field retention retentionpolicy.runtime public inte ce setvalue以上就是乙個自定義的註解,下面來進行說明。target elementtype.field 表示支援該註解的程式元素,field就是屬性...

自定義註解

三個重要元註解 target 即註解的作用域,用於說明註解的使用範圍 即註解可以用在什麼地方,比如類的註解,方法註解,成員變數註解等等 elemenettype.constructor 構造器宣告 elemenettype.field 域宣告 包括 enum 例項 elemenettype.loca...

自定義註解

三個重要元註解 target 即註解的作用域,用於說明註解的使用範圍 即註解可以用在什麼地方,比如類的註解,方法註解,成員變數註解等等 elemenettype.constructor 構造器宣告 elemenettype.field 域宣告 包括 enum 例項 elemenettype.loca...