springBoot启动时自动校验配置
一,实体类配置:
package com.readyhuihui.testspring.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.validation.annotation.Validated;
import javax.validation.constraints.NotNull;
/**
* @description 配置类
* @author liyonghui
* @date 2021/2/24 10:31
**/
@Validated
@Configuration
@EnableConfigurationProperties(Config.class)
@ConfigurationProperties(prefix = "config")
public class Config {
/**
* 媒体云环境媒体云租户标识
*/
@NotNull(message = "配置文件配置必须要配置[config.to-company-id]属性")
private String toCompanyId;
/**
* 媒体云环境媒体云用户标识
*/
@NotNull(message = "配置文件配置必须要配置[config.to-user-id]属性")
private String toUserId;
/**
* 媒体云环境媒体云接口地址
*/
@NotNull(message = "配置文件配置必须要配置[config.to-interface-url]属性")
private String toInterfaceUrl;
/**
* 数据来源
*/
@NotNull(message = "配置文件配置必须要配置[config.source]属性")
private String source;
/**
* ftp地址
*/
@NotNull(message = "配置文件配置必须要配置[config.ftp-host-name]属性")
private String ftpHostName;
/**
* ftp端口
*/
@NotNull(message = "配置文件配置必须要配置[config.ftp-port]属性")
private Integer ftpPort;
/**
* ftp用户名
*/
@NotNull(message = "配置文件配置必须要配置[config.ftp-user-name]属性")
private String ftpUserName;
/**
* ftp密码
*/
@NotNull(message = "配置文件配置必须要配置[config.ftp-pass-word]属性")
private String ftpPassWord;
/**
* ftp文件路径
*/
@NotNull(message = "配置文件配置必须要配置[config.ftp-bash-path]属性")
private String ftpBashPath;
/**
* 开启扫描 open
*/
@NotNull(message = "配置文件配置必须要配置[config.is-open-scan]属性")
private String isOpenScan;
public String getToCompanyId() {
return toCompanyId;
}
public void setToCompanyId(String toCompanyId) {
this.toCompanyId = toCompanyId;
}
public String getToUserId() {
return toUserId;
}
public void setToUserId(String toUserId) {
this.toUserId = toUserId;
}
public String getToInterfaceUrl() {
return toInterfaceUrl;
}
public void setToInterfaceUrl(String toInterfaceUrl) {
this.toInterfaceUrl = toInterfaceUrl;
}
public String getSource() {
return source;
}
public void setSource(String source) {
this.source = source;
}
public String getFtpHostName() {
return ftpHostName;
}
public void setFtpHostName(String ftpHostName) {
this.ftpHostName = ftpHostName;
}
public Integer getFtpPort() {
return ftpPort;
}
public void setFtpPort(Integer ftpPort) {
this.ftpPort = ftpPort;
}
public String getFtpUserName() {
return ftpUserName;
}
public void setFtpUserName(String ftpUserName) {
this.ftpUserName = ftpUserName;
}
public String getFtpPassWord() {
return ftpPassWord;
}
public void setFtpPassWord(String ftpPassWord) {
this.ftpPassWord = ftpPassWord;
}
public String getFtpBashPath() {
return ftpBashPath;
}
public void setFtpBashPath(String ftpBashPath) {
this.ftpBashPath = ftpBashPath;
}
public String getIsOpenScan() {
return isOpenScan;
}
public void setIsOpenScan(String isOpenScan) {
this.isOpenScan = isOpenScan;
}
@Override
public String toString() {
return "Config{" +
"toCompanyId='" + toCompanyId + '\'' +
", toUserId='" + toUserId + '\'' +
", toInterfaceUrl='" + toInterfaceUrl + '\'' +
", source='" + source + '\'' +
", ftpHostName='" + ftpHostName + '\'' +
", ftpPort=" + ftpPort +
", ftpUserName='" + ftpUserName + '\'' +
", ftpPassWord='" + ftpPassWord + '\'' +
", ftpBashPath='" + ftpBashPath + '\'' +
", isOpenScan='" + isOpenScan + '\'' +
'}';
}
}
二,pom.xml配置【具体版本请根据实际的springboot项目版本设置,目前我用的springboot版本是:2.4.3】
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.3.5.Final</version>
</dependency>
三:启动后日志打印内容如下:
Binding to target org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'config' to com.cdvcloud.mtysynmediaother.config.Config$$EnhancerBySpringCGLIB$$3bc107a7 failed:
Property: config.source
Value: null
Reason: 配置文件配置必须要配置[config.source]属性
Property: config.ftpUserName
Value: null
Reason: 配置文件配置必须要配置[config.ftp-user-name]属性
Property: config.ftpPort
Value: null
Reason: 配置文件配置必须要配置[config.ftp-port]属性
Property: config.isOpenScan
Value: null
Reason: 配置文件配置必须要配置[config.is-open-scan]属性
Property: config.toUserId
Value: null
Reason: 配置文件配置必须要配置[config.to-user-id]属性
Property: config.ftpPassWord
Value: null
Reason: 配置文件配置必须要配置[config.ftp-pass-word]属性
Property: config.ftpHostName
Value: null
Reason: 配置文件配置必须要配置[config.ftp-host-name]属性
Property: config.ftpBashPath
Value: null
Reason: 配置文件配置必须要配置[config.ftp-bash-path]属性
Property: config.toCompanyId
Value: null
Reason: 配置文件配置必须要配置[config.to-company-id]属性
Property: config.toInterfaceUrl
Value: null
Reason: 配置文件配置必须要配置[config.to-interface-url]属性
Action:
Update your application's configuration

浙公网安备 33010602011771号