日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

SpringBoot - 优雅的实现【自定义参数校验】高级进阶

發布時間:2025/3/21 javascript 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SpringBoot - 优雅的实现【自定义参数校验】高级进阶 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

  • Pre
  • 概述
  • 三部曲
    • Step1 搞自定義注解
    • Step2 搞校驗邏輯
    • Step3 使用
    • Step4 驗證
  • 附 int 類型的判斷
  • 源碼


Pre

SpringBoot - 優雅的實現【參數校驗】高級進階

SpringBoot - 優雅的實現【自定義參數校驗】高級進階

SpringBoot - 優雅的實現【參數分組校驗】高級進階

SpringBoot - 使用Assert校驗讓業務代碼更簡潔


概述

接上文, Spring Validation 提供的注解基本上夠用,但是復雜的校驗,我們還是需要自己定義注解來實現自動校驗。

舉個例子: 實體類中的sex性別屬性 , 只能輸入 M 、 F 這2個枚舉值


三部曲

Step1 搞自定義注解

package com.artisan.customvalidator;import javax.validation.Constraint; import javax.validation.Payload; import java.lang.annotation.Documented; import java.lang.annotation.Repeatable; import java.lang.annotation.Retention; import java.lang.annotation.Target;import static java.lang.annotation.ElementType.*; import static java.lang.annotation.RetentionPolicy.RUNTIME;/*** @author 小工匠* @version 1.0* @mark: show me the code , change the world*/@Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE}) @Retention(RUNTIME) @Repeatable(EnumString.List.class) @Documented @Constraint(validatedBy = EnumStringValidator.class)//標明由哪個類執行校驗邏輯 public @interface EnumString {String message() default "value not in enum values.";Class<?>[] groups() default {};Class<? extends Payload>[] payload() default {};/*** @return date must in this value array*/String[] value();/*** Defines several {@link EnumString} annotations on the same element.** @see EnumString*/@Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE})@Retention(RUNTIME)@Documented@interface List {EnumString[] value();} }

Step2 搞校驗邏輯

package com.artisan.customvalidator;import lombok.extern.slf4j.Slf4j;import javax.validation.ConstraintValidator; import javax.validation.ConstraintValidatorContext; import java.util.Arrays; import java.util.List;/*** @author 小工匠* @version 1.0* @mark: show me the code , change the world*/ @Slf4j public class EnumStringValidator implements ConstraintValidator<EnumString, String> {private List<String> enumStringList;@Overridepublic void initialize(EnumString constraintAnnotation) {log.info("EnumStringValidator initialize.....");enumStringList = Arrays.asList(constraintAnnotation.value());}@Overridepublic boolean isValid(String value, ConstraintValidatorContext context) {if (value == null) {return true;}return enumStringList.contains(value);} }

Step3 使用

在字段上增加注解

Step4 驗證


附 int 類型的判斷

package com.artisan.customvalidator;import javax.validation.Constraint; import javax.validation.Payload; import java.lang.annotation.Documented; import java.lang.annotation.Repeatable; import java.lang.annotation.Retention; import java.lang.annotation.Target;import static java.lang.annotation.ElementType.*; import static java.lang.annotation.RetentionPolicy.RUNTIME;/*** @author 小工匠* @version 1.0* @Des 擴展int枚舉校驗* @mark: show me the code , change the world*/ @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE}) @Retention(RUNTIME) @Repeatable(EnumInteger.List.class) @Documented @Constraint(validatedBy = EnumNumberValidator.class) public @interface EnumInteger {String message() default "value not in enum values.";Class<?>[] groups() default {};Class<? extends Payload>[] payload() default {};/*** @return date must in this value array*/int[] value();/*** Defines several {@link EnumInteger} annotations on the same element.** @see EnumInteger*/@Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE})@Retention(RUNTIME)@Documented@interface List {EnumInteger[] value();} } package com.artisan.customvalidator;import javax.validation.ConstraintValidator; import javax.validation.ConstraintValidatorContext; import java.util.ArrayList; import java.util.List;/*** @author 小工匠* @version 1.0* @mark: show me the code , change the world*/ public class EnumNumberValidator implements ConstraintValidator<EnumInteger, Number> {private List<Integer> enumStringList;@Overridepublic void initialize(EnumInteger constraintAnnotation) {enumStringList = new ArrayList<>();int[] values = constraintAnnotation.value();for (int value : values) {enumStringList.add(value);}}@Overridepublic boolean isValid(Number value, ConstraintValidatorContext context) {if (value == null) {return true;}return enumStringList.contains(value.intValue());} }

源碼

https://github.com/yangshangwei/boot2

總結

以上是生活随笔為你收集整理的SpringBoot - 优雅的实现【自定义参数校验】高级进阶的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。