<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          spv簡單的 Spring 參數(shù)驗證庫

          聯(lián)合創(chuàng)作 · 2023-09-28 10:56

          springmvc springboot 簡單輕量好用的參數(shù)驗證。支持基本類型查參數(shù)驗證。支持自定義注解的參數(shù)驗證。使用簡單方便。

          使用4步配置

          1、導入jar

                  <dependency>
                      <groupId>com.github.fashionbrot</groupId>
                      <artifactId>mars-validated</artifactId>
                      <version>1.0.0</version>
                  </dependency>

          2、使用注解

          2.1 springboot 配置

          @SpringBootApplication
          @EnableValidatedConfig(fileName = "valid_zh_CN")
          public class DemoApplication {
              public static void main(String[] args) {
                  SpringApplication.run(DemoApplication.class, args);
              }
          }

          2.2 spring配置

          @Component
          @Configuration
          @EnableValidatedConfig(fileName = "valid_zh_CN")
          public class Config {
          
          
          }
          

          3、使用 @Validated 開啟接口驗證 @Email驗證郵箱格式

          @Controller
          public class DemoController {
          
              @Autowired
              private TestService testService;
              
          
              @RequestMapping("/emailCheck")
              @ResponseBody
              @Validated //注意此處
              public String demo(@Email String abc,){
                  return testService.test(abc);
              }
          
          }   
          @Controller
          public class DemoController {
          
              @Autowired
              private TestService testService;
              
              
              @RequestMapping("/idcardCheck")
              @ResponseBody
              @Validated
              public String demo(IdCardModel idCardModel){
                  return testService.test("ac");
              }
              
              @RequestMapping("/idcardCheck2")
              @ResponseBody
              public String demo2(IdCardModel idCardModel){
                  return testService.test2("ac");
              }
          
          
          }
          
          **此處支持多繼承驗證*** 
          
          public class IdCardModel extends BaseModel{
          
              @IdCard
              private String idCardModel;
          
              public String getIdCardModel() {
                  return idCardModel;
              }
          
              public void setIdCardModel(String idCardModel) {
                  this.idCardModel = idCardModel;
              }
          
          }
          
          @Service
          public class TestService{
          
              @Validated
              public void test2(@IdCard String abc){
          
              }
          
          }
          
          

          4、自定義實現(xiàn)全局異常處理

          攔截 com.fashion.spv.validated.exception.ValidatedException

          @RestControllerAdvice
          public class GlobalExceptionHandler {
          
               
              @ExceptionHandler(ValidatedException.class)
              @ResponseStatus(HttpStatus.OK)
              public RespMessage ValidationException(ValidatedException e){
          
                  return new RespMessage(-100,e.getMsg());
              }
          
          }
          

          validated 參數(shù)驗證

          使用環(huán)境

          spring4.0 及以上
          jdk1.8 及以上

          Annotation Supported data types 作用
          NotBlank String 驗證String 字符串是否為空
          NotNull String,Object,Integer,Long,Double,Short,Float,BigDecimal, BigInteger 驗證對象是否為空
          AssertFalse Boolean,boolean,String 只能為false
          AssertTrue Boolean,boolean,String 只能為true
          BankCard String 驗證銀行卡
          CreditCard String 驗證信用卡
          Default Integer,Double,Long,Short,Float,BigDecimal,String 設置默認值
          Digits String 驗證是否是數(shù)字
          Email String 驗證是否是郵箱
          IdCard String 驗證是否是身份證,驗證18歲
          Length int,long,short,double,Integer,Long,Float,Double,Short,String 驗證長度
          Pattern String 正則表達式驗證
          Phone String 驗證手機號是否正確
          Size int,long,short,Integer,Long,Short 驗證大小值

          支持自定義注解 如下:

          1、實現(xiàn)  ConstraintValidator 此接口
          2、自定義注解如下:  CustomConstraintValidator.class,CustomConstraintValidator2.class 實現(xiàn)類可多個,至少有一個
          
              @Documented
              @Target({ElementType.FIELD,  ElementType.PARAMETER})
              @Retention(RetentionPolicy.RUNTIME)
              @Constraint(validatedBy = {CustomConstraintValidator.class,CustomConstraintValidator2.class})
              public @interface Custom {
              
                  String msg() default "com.spv.valid.Custom.msg";
              
                  int min();
              }
          
          
          3、代碼實現(xiàn)
          
          
          
          public class CustomConstraintValidator implements ConstraintValidator<Custom,String> {
          
              @Override
              public boolean isValid(Custom custom,String var1) {
                  /**
                   * 自定義方法
                   */
                  int min=custom.min();
                  /**
                   * value
                   */
                  System.out.println(var1);
                  /**
                   * return true 則驗證成功 false 驗證失敗
                    */
                  return true;
              }
          }
          瀏覽 16
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          編輯 分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          編輯 分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  欧美色图第五页 | 美女免费AV| 欧美日韩视频在线播放 | 在线观看中文字幕 | 人人人人人人摸 |