<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>

          Springboot中優(yōu)雅進(jìn)行字段校驗(yàn)

          共 2860字,需瀏覽 6分鐘

           ·

          2021-11-15 03:25

          轉(zhuǎn)自:何甜甜在嗎

          鏈接:https://juejin.cn/post/6913735652806754311


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


          | Controller層 VS Service層

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

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

          | 常用校驗(yàn)工具類(lèi)

          使用Hibernate Validate

          引入依賴(lài)


          ????org.hibernate
          ????hibernate-validator
          ????4.3.1.Final?

          常用注解說(shuō)明

          使用姿勢(shì)

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

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

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


          ~?定義一個(gè)實(shí)體:


          public?class?DataSetSaveVO?{
          ????//唯一標(biāo)識(shí)符為空
          ????@NotBlank(message?=?"user?uuid?is?empty")
          ????//用戶(hù)名稱(chēng)只能是字母和數(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ù)集名稱(chēng)只能是字母和數(shù)字
          ????@Pattern(regexp?=?"^[A-Za-z0-9]+$",?message?=?"data?set?names?can?only?be?letters?and?Numbers")
          ????//文件名稱(chēng)過(guò)長(zhǎng)
          ????@Length(max?=?48,?message?=?"file?name?too?long")
          ????//文件名稱(chēng)為空
          ????@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;
          }

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


          ~?Controller層中的方法:


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

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

          使用commons-lang3

          引入依賴(lài)


          ????org.apache.commons
          ????commons-lang3
          ????3.4

          常用方法說(shuō)明

          測(cè)試代碼

          //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?emptyList?=?new?ArrayList<>();
          List?nullList?=?null;
          List?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)上面的方面都無(wú)法滿(mǎn)足校驗(yàn)的需求以后,可以考慮使用自定義注解。如何寫(xiě)一個(gè)自定義注解,可以參考本文作者之前的文章:《Spring自定義注解從入門(mén)到精通》。

          有道無(wú)術(shù),術(shù)可成;有術(shù)無(wú)道,止于術(shù)

          歡迎大家關(guān)注Java之道公眾號(hào)


          好文章,我在看??

          瀏覽 36
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <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无码专区在线播放中文 | www.一区二区三区 | 爱骚逼黄色视频 |