Springboot 自定義校驗代碼實例
更新時間:2019年11月26日 15:01:48 作者:Bean_zheng
這篇文章主要介紹了Springboot 自定義校驗代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
這篇文章主要介紹了Springboot 自定義校驗代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
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; } }
運用
@StartWithValidation(message = "Param 'address' must be start with 'Beijing'.", start = "Beijing") public String address;
解釋:
地址必須以Beijing開頭,否則校驗失敗
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Flink開發(fā)IDEA環(huán)境搭建與測試的方法
這篇文章主要介紹了Flink開發(fā)IDEA環(huán)境搭建與測試的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-03-03Java實現(xiàn)字符串的分割(基于String.split()方法)
Java中的我們可以利用split把字符串按照指定的分割符進行分割,然后返回字符串數(shù)組,下面這篇文章主要給大家介紹了關(guān)于Java實現(xiàn)字符串的分割的相關(guān)資料,是基于jDK1.8版本中的String.split()方法,需要的朋友可以參考下2022-09-09Redisson分布式信號量RSemaphore的使用超詳細講解
這篇文章主要介紹了Redisson分布式信號量RSemaphore的使用,基于Redis的Redisson的分布式信號量RSemaphore采用了與java.util.concurrent.Semaphore相似的接口和用法2023-02-02HTTP 415錯誤-Unsupported media type詳解
這篇文章主要介紹了HTTP 415錯誤-Unsupported media type詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-08-08