1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        Spring Boot 進(jìn)行優(yōu)雅的字段校驗(yàn),寫得太好了!

        共 4731字,需瀏覽 10分鐘

         ·

        2021-11-15 05:25


        作者 | 何甜甜在嗎

        來源 | juejin.cn/post/6913735652806754311

        前段時間提交代碼審核,同事提了一個代碼規(guī)范缺陷:參數(shù)校驗(yàn)應(yīng)該放在controller層。到底應(yīng)該如何做參數(shù)校驗(yàn)?zāi)?/strong>

        Controller層 VS Service層

        去網(wǎng)上查閱了一些資料,一般推薦與業(yè)務(wù)無關(guān)的放在Controller層中進(jìn)行校驗(yàn),而與業(yè)務(wù)有關(guān)的放在Service層中進(jìn)行校驗(yàn)。

        那么如何將參數(shù)校驗(yàn)寫的優(yōu)雅美觀呢,如果都是if - else,就感覺代碼寫的很low,還好有輪子可以使用

        如果您正在學(xué)習(xí)Spring Boot,推薦一個連載多年還在繼續(xù)更新的免費(fèi)教程:http://blog.didispace.com/spring-boot-learning-2x/

        常用校驗(yàn)工具類

        使用Hibernate Validate

        引入依賴

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>4.3.1.Final</version> 
        </dependency>

        常用注解說明

        使用姿勢

        需要搭配在Controller中搭配@Validated或@Valid注解一起使用,@Validated和@Valid注解區(qū)別不是很大,一般情況下任選一個即可,區(qū)別如下:

        雖然@Validated比@Valid更加強(qiáng)大,在@Valid之上提供了分組功能和驗(yàn)證排序功能,不過在實(shí)際項(xiàng)目中一直沒有用到過

        Hibernate-validate框架中的注解是需要加在實(shí)體中一起使用的

        定義一個實(shí)體

        public class DataSetSaveVO {
            //唯一標(biāo)識符為空
            @NotBlank(message = "user uuid is empty")
            //用戶名稱只能是字母和數(shù)字
            @Pattern(regexp = "^[a-z0-9]+$", message = "user names can only be alphabetic and numeric")
            @Length(max = 48, message = "user uuid length over 48 byte")
            private String userUuid;

            //數(shù)據(jù)集名稱只能是字母和數(shù)字
            @Pattern(regexp = "^[A-Za-z0-9]+$", message = "data set names can only be letters and Numbers")
            //文件名稱過長
            @Length(max = 48, message = "file name too long")
            //文件名稱為空
            @NotBlank(message = "file name is empty")
            private String name;

            //數(shù)據(jù)集描述最多為256字節(jié)
            @Length(max = 256, message = "data set description length over 256 byte")
            //數(shù)據(jù)集描述為空
            @NotBlank(message = "data set description is null")
            private String description;
        }

        說明:message字段為不符合校驗(yàn)規(guī)則時拋出的異常信息

        Controller層中的方法

        @PostMapping
        public ResponseVO createDataSet(@Valid @RequestBody DataSetSaveVO dataSetVO) {
            return ResponseUtil.success(dataSetService.saveDataSet(dataSetVO));
        }

        說明:在校驗(yàn)的實(shí)體DataSetSaveVO旁邊添加@Valid或@Validated注解

        如果您正在學(xué)習(xí)Spring Cloud,推薦一個連載多年還在繼續(xù)更新的免費(fèi)教程:https://blog.didispace.com/spring-cloud-learning/

        使用commons-lang3

        引入依賴

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.4</version>
        </dependency>

        常用方法說明

        測試代碼

        //StringUtils.isEmpty
        System.out.println(StringUtils.isEmpty(""));  //true
        System.out.println(StringUtils.isEmpty("  "));  //false
        //StringUtils.isNotEmpty
        System.out.println(StringUtils.isNotEmpty(""));  //false
                
        //StringUtils.isBlank
        System.out.println(StringUtils.isBlank(""));  //true
        System.out.println(StringUtils.isBlank(" "));  //true
        //StringUtils.isNotBlank
        System.out.println(StringUtils.isNotBlank(" "));  //false

        List<Integer> emptyList = new ArrayList<>();
        List<Integer> nullList = null;
        List<Integer> notEmptyList = new ArrayList<>();
        notEmptyList.add(1);

        //CollectionUtils.isEmpty
        System.out.println(CollectionUtils.isEmpty(emptyList));   //true
        System.out.println(CollectionUtils.isEmpty(nullList));   //true
        System.out.println(CollectionUtils.isEmpty(notEmptyList));   //false

        //CollectionUtils.isNotEmpty
        System.out.println(CollectionUtils.isNotEmpty(emptyList));   //false
        System.out.println(CollectionUtils.isNotEmpty(nullList));   //false
        System.out.println(CollectionUtils.isNotEmpty(notEmptyList));   //true

        自定義注解

        當(dāng)上面的方面都無法滿足校驗(yàn)的需求以后,可以考慮使用自定義注解。

        往期推薦

        浪潮的加班標(biāo)語炸了,這是頂風(fēng)作案?網(wǎng)傳:1月加班87小時還要扣工資?

        如果MySQL的自增 ID 用完了,怎么辦?

        手磨14nm咖啡,傳播性病給60%的實(shí)習(xí)團(tuán)隊(duì)!這樣的簡歷,90%的公司拋出橄欖枝?

        我精通各種技術(shù)體系,因已45歲求職難!

        朝陽群眾舉報阿里996造成交通嚴(yán)重堵塞!網(wǎng)友:誰舉報下我們啊...



        技術(shù)交流群

        最近有很多人問,有沒有讀者交流群,想知道怎么加入。加入方式很簡單,有興趣的同學(xué),只需要點(diǎn)擊下方卡片,回復(fù)“加群,即可免費(fèi)加入我們的高質(zhì)量技術(shù)交流群!

        點(diǎn)擊閱讀原文,送你免費(fèi)Spring Boot教程!

        瀏覽 33
        點(diǎn)贊
        評論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報
        評論
        圖片
        表情
        推薦
        點(diǎn)贊
        評論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

          <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            大香蕉久久久久 | 国产美女做爰免费视频爱酱app | 性色av蜜臀av高清久久苏然 | 年轻少妇ass下部精品 | 午夜羞羞免费视频 | 小坏蛋再深点灬舒服灬太久了视频 | 超碰人人欧美 | 国产精品片18区乱婬人成人 | 国产精品成人无码a v在线观看 | 青青草肏逼视频 |