Springboot 自定義校驗(yàn)代碼實(shí)例
這篇文章主要介紹了Springboot 自定義校驗(yàn)代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
StartWithValidation.class
@Documented @Constraint(validatedBy = StartWithValidator.class ) @Target({METHOD, FIELD}) @Retention(RUNTIME) public @interface StartWithValidation { String message() default "不是正確的性別取值范圍"; String start() default "_"; Class<?>[] groups() default {}; Class<? extends Payload>[] payload() default {}; @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER}) @Retention(RUNTIME) @Documented @interface List { StartWithValidation[] value(); } }
StartWithValidator.class
public class StartWithValidator implements ConstraintValidator<StartWithValidation, String> { private String start; @Override public void initialize(StartWithValidation constraintAnnotation) { start = constraintAnnotation.start(); } @Override public boolean isValid(String value, ConstraintValidatorContext context) { if (!StringUtils.isEmpty(value)) { return value.startsWith(start); } return true; } }
運(yùn)用
@StartWithValidation(message = "Param 'address' must be start with 'Beijing'.", start = "Beijing") public String address;
解釋:
地址必須以Beijing開頭,否則校驗(yàn)失敗
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Flink開發(fā)IDEA環(huán)境搭建與測(cè)試的方法
這篇文章主要介紹了Flink開發(fā)IDEA環(huán)境搭建與測(cè)試的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03K均值聚類算法的Java版實(shí)現(xiàn)代碼示例
這篇文章主要介紹了K均值聚類算法的Java版實(shí)現(xiàn)代碼示例,具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-12-12Java實(shí)現(xiàn)字符串的分割(基于String.split()方法)
Java中的我們可以利用split把字符串按照指定的分割符進(jìn)行分割,然后返回字符串?dāng)?shù)組,下面這篇文章主要給大家介紹了關(guān)于Java實(shí)現(xiàn)字符串的分割的相關(guān)資料,是基于jDK1.8版本中的String.split()方法,需要的朋友可以參考下2022-09-09Redisson分布式信號(hào)量RSemaphore的使用超詳細(xì)講解
這篇文章主要介紹了Redisson分布式信號(hào)量RSemaphore的使用,基于Redis的Redisson的分布式信號(hào)量RSemaphore采用了與java.util.concurrent.Semaphore相似的接口和用法2023-02-02HTTP 415錯(cuò)誤-Unsupported media type詳解
這篇文章主要介紹了HTTP 415錯(cuò)誤-Unsupported media type詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08