介紹
在日常的介面開發中,為了防止非法參數對業務造成影響,經常需要對介面的參數進行校驗,例如登入的時候需要校驗使用者名稱和密碼是否為空,新增用戶的時候校驗用戶信箱地址、手機號碼格式是否正確。靠程式碼對介面參數一個個校驗的話就太繁瑣了,程式碼可讀性極差。
Validator
框架就是為了解決開發人員在開發的時候少寫程式碼,提升開發效率;Validator專門用來進行介面參數校驗,例如常見的必填校驗,email格式校驗,使用者名稱必須位於6到12之間等等。
接下來我們來看看在SpringbBoot中如何整合參數校驗框架。
1.SpringBoot中整合參數校驗
1.1引入依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-validation</artifactId> </dependency>
1.2定義參數實體類別
package com.didiplus.modules.sys.domain; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.Data; import javax.validation.constraints.Email; import javax.validation.constraints.NotBlank; import javax.validation.constraints.NotEmpty; /** * Author: didiplus * Email: 972479352@qq.com * CreateTime: 2022/4/25 * Desc: 字典类型领域模型 */ @Data @ApiModel(value = "字典类型") public class SysDictType { @ApiModelProperty("ID") private String id; @NotBlank(message = "字典名称必填项") @ApiModelProperty(value = "字典名称",example = "用户ID") private String typeName; @NotBlank(message = "字典编码不能为空") @ApiModelProperty(value = "字典编码") private String typeCode; @Email(message = "请填写正确的邮箱地址") @ApiModelProperty(value = "字典编码") private String email; @ApiModelProperty(value = "字典描述") private String description; @NotBlank(message = "字典状态不能为空") @ApiModelProperty(value = "字典状态") private String enable; }
常見的限制註解如下:
註解 | 功能 |
---|---|
#@AssertFalse | 可以為null,如果不為null的話必須為false |
@AssertTrue | 可以為null,如果不為null的話必須為true |
@DecimalMax | 設定不能超過最大值 |
@DecimalMin | #設定不能超過最小值 |
@Digits | 設定必須是數字且數字整數的位數和小數的位數必須在指定範圍內 |
@Future | ##日期必須在目前日期的未來|
#日期必須在目前日期的過去 | |
最大不得超過此最大值 | |
最大值不得小於此最小值 | ##@NotNull |
@Null | |
@Pattern | |
@Size | |
#@Length | |
@NotBlank | |
@NotEmpty | |
@Range | |
@URL | |
以上是SpringBoot怎麼進行參數校驗的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Javaremainsagoodlanguageduetoitscontinuousevolutionandrobustecosystem.1)Lambdaexpressionsenhancecodereadabilityandenablefunctionalprogramming.2)Streamsallowforefficientdataprocessing,particularlywithlargedatasets.3)ThemodularsystemintroducedinJava9im

Javaisgreatduetoitsplatformindependence,robustOOPsupport,extensivelibraries,andstrongcommunity.1)PlatformindependenceviaJVMallowscodetorunonvariousplatforms.2)OOPfeatureslikeencapsulation,inheritance,andpolymorphismenablemodularandscalablecode.3)Rich

Java的五大特色是多態性、Lambda表達式、StreamsAPI、泛型和異常處理。 1.多態性讓不同類的對象可以作為共同基類的對象使用。 2.Lambda表達式使代碼更簡潔,特別適合處理集合和流。 3.StreamsAPI高效處理大數據集,支持聲明式操作。 4.泛型提供類型安全和重用性,編譯時捕獲類型錯誤。 5.異常處理幫助優雅處理錯誤,編寫可靠軟件。

java'stopfeatureSnificallyenhanceItsperformanCandScalability.1)對象 - 方向clincipleslike-polymormormormormormormormormormormormorableableflexibleandscalablecode.2)garbageCollectionAutectionAutoctionAutoctionAutoctionAutoctionAutoctionAutoMenateMememorymanateMmanateMmanateMmanagementButCancausElatemention.3)

JVM的核心組件包括ClassLoader、RuntimeDataArea和ExecutionEngine。 1)ClassLoader負責加載、鏈接和初始化類和接口。 2)RuntimeDataArea包含MethodArea、Heap、Stack、PCRegister和NativeMethodStacks。 3)ExecutionEngine由Interpreter、JITCompiler和GarbageCollector組成,負責bytecode的執行和優化。

Java'ssafetyandsecurityarebolsteredby:1)strongtyping,whichpreventstype-relatederrors;2)automaticmemorymanagementviagarbagecollection,reducingmemory-relatedvulnerabilities;3)sandboxing,isolatingcodefromthesystem;and4)robustexceptionhandling,ensuringgr

Javaoffersseveralkeyfeaturesthatenhancecodingskills:1)對象 - 方向 - 方向上的allowslowsmodelowsmodelingreal-worldentities

thejvmisacrucialcomponentthatrunsjavacodebytranslatingitolachine特定結構,影響性能,安全性和便攜性。 1)theclassloaderloader,links andinitializesClasses.2)theexecutionEngineExecutionEngineExecutionEngineExecuteNexeCuteByteCuteByteCuteByTecuteByteCuteByteCuteBytecuteBytecuteByteCoDeinintolachineinstructionsions.3)Memo.3)Memo


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

記事本++7.3.1
好用且免費的程式碼編輯器

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。