SpringBoot數(shù)據(jù)校驗(yàn)及多環(huán)境配置的問題詳解
接上節(jié),本節(jié)補(bǔ)充一下數(shù)據(jù)校驗(yàn)及多環(huán)境配置的內(nèi)容,仍是 SpringBoot-02-Config 項(xiàng)目。
1. 數(shù)據(jù)校驗(yàn)
使用數(shù)據(jù)校驗(yàn),可以在輸入不合法數(shù)據(jù)時(shí)拋出異常,首先要添加 validation
的依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-validation</artifactId> </dependency> <dependency> <groupId>javax.validation</groupId> <artifactId>validation-api</artifactId> <version>2.0.1.Final</version> </dependency>
在之前的 Person 類上使用 @Validated
注解開啟數(shù)據(jù)校驗(yàn),在 name 屬性上添加 @Email
注解,表明這個(gè)屬性要符合 Email 的格式
@Data @AllArgsConstructor @NoArgsConstructor @Component //注冊為 bean @ConfigurationProperties(prefix = "person") // 開啟數(shù)據(jù)校驗(yàn) @Validated public class Person { // 檢查 name 符合郵箱格式 @Email() private String name; private Integer age; private Boolean happy; private Date birth; private Map<String,Object> maps; private List<Object> lists; private Dog dog; }
配置文件中注入的 name 屬性為 qiyuan,是不合法的,這時(shí)運(yùn)行測試方法,SpringBoot 會(huì)報(bào)錯(cuò)
Binding to target org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'person' to com.qiyuan.entity.Person failed:
Property: person.name
Value: qiyuan
Origin: class path resource [application.yaml] - 2:9
Reason: 不是一個(gè)合法的電子郵件地址
查看底層的錯(cuò)誤,也可以看到
Caused by: org.springframework.boot.context.properties.bind.validation.BindValidationException: Binding validation errors on person
- Field error in object 'person' on field 'name': rejected value [qiyuan]; codes [Email.person.name,Email.name,Email.java.lang.String,Email]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [person.name,name]; arguments []; default message [name],[Ljavax.validation.constraints.Pattern$Flag;@44f3fe83,.*]; default message [不是一個(gè)合法的電子郵件地址]; origin class path resource [application.yaml] - 2:9
總而言之,使用數(shù)據(jù)校驗(yàn)可以方便地對屬性的值進(jìn)行合法性檢測,在 JSR303 規(guī)范中( Java Specification Requests,即 Java 規(guī)范提案,JSR-303 是 JAVA EE 6 中的一項(xiàng)子規(guī)范)還有許多這樣的檢測注釋,用到的時(shí)候再查吧!
2. 多環(huán)境配置
在 Spring 中可以使用 profile 對不同的環(huán)境進(jìn)行不同的配置設(shè)置,通過激活不同的環(huán)境版本,實(shí)現(xiàn)快速切換環(huán)境。
在編寫配置文件的時(shí)候,文件名可以是 application-{profile}.properties/yml
,通過不同的 profile 指定不同的環(huán)境,如 application-test.properties
表示測試環(huán)境,application-dev.properties
表示開發(fā)環(huán)境;但 SpringBoot 不會(huì)直接使用這種配置文件,它默認(rèn)使用的是 application.properties
配置文件,所以需要指定需要使用的環(huán)境
spring.profiles.active=dev
若使用 yaml 進(jìn)行配置,則更加簡單了;yaml 提供了多文檔塊功能,不用創(chuàng)建多個(gè)配置文件
server: port: 8081 #選擇要激活那個(gè)環(huán)境塊 spring: profiles: active: test --- server: port: 8082 spring: profiles: dev #配置環(huán)境的名稱 --- server: port: 8083 spring: profiles: prod #配置環(huán)境的名稱
注意:如果 properties 和 yaml 都進(jìn)行了端口配置,且沒有指定其他配置,會(huì)默認(rèn)使用 properties 配置文件。
3. 配置文件加載位置
SpringBoot 會(huì)掃描以下位置的 application.properties
或 application.yml
文件作為默認(rèn)配置文件,優(yōu)先級順序?yàn)?/p>
- 項(xiàng)目路徑下的 config 文件夾中的配置文件:
file:./config/
- 項(xiàng)目路徑下的配置文件:
file:./
- 資源路徑下的 config 文件夾中的配置文件:
classpath:./config/
- 資源路徑下的配置文件:
classpath:./
優(yōu)先級由高到底,高優(yōu)先級的配置會(huì)覆蓋低優(yōu)先級的配置;若沒有沖突,則配置會(huì)互補(bǔ)!
4. 總結(jié)
到此這篇關(guān)于SpringBoot數(shù)據(jù)校驗(yàn)及多環(huán)境配置的文章就介紹到這了,更多相關(guān)SpringBoot數(shù)據(jù)校驗(yàn)多環(huán)境配置內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot使用Redis緩存的實(shí)現(xiàn)方法
這篇文章主要介紹了SpringBoot使用Redis緩存的實(shí)現(xiàn)方法,需要的朋友可以參考下2018-02-02java基本教程之常用的實(shí)現(xiàn)多線程的兩種方式 java多線程教程
下面開始學(xué)習(xí)“常用的實(shí)現(xiàn)多線程的2種方式”:Thread 和 Runnable。之所以說是常用的,是因?yàn)橥ㄟ^還可以通過java.util.concurrent包中的線程池來實(shí)現(xiàn)多線程2014-01-01Spring Cloud Zuul路由規(guī)則動(dòng)態(tài)更新解析
這篇文章主要介紹了Spring Cloud Zuul路由規(guī)則動(dòng)態(tài)更新解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11tio-boot框架整合ehcache實(shí)現(xiàn)過程示例
這篇文章主要為大家介紹了tio-boot框架整合ehcache實(shí)現(xiàn)過程示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12springboot實(shí)現(xiàn)極驗(yàn)校驗(yàn)的項(xiàng)目實(shí)踐
在系統(tǒng)業(yè)務(wù)中,需要想客戶發(fā)送手機(jī)驗(yàn)證碼,進(jìn)行驗(yàn)證后,才能提交,本文主要介紹了springboot實(shí)現(xiàn)極驗(yàn)校驗(yàn)的項(xiàng)目實(shí)踐,具有一定的參考價(jià)值,感興趣的可以了解一下2023-09-09