Nacos中yaml文件新增配置项不规范导致项目启动失败
有一个邮件提醒的需求,邮件标题、内容模板通过Nacos配置中心的配置文件进行配置(@RefreshScope实时刷新),配置后,服务启动报错。
配置如下:
individualWarning:title: ××到期提醒content: 尊敬的{},您好,您有{}笔...业务...请注意查看。
具体错误如下:
Configuration property name 'individualWarning' is not valid:Invalid characters: 'W'
Bean: ××××.individualWarningProperties
Reason: Canonical names should be kebab-case ('-' separated), lowercase alpha-numeric characters and must start with a letterAction:Modify 'individualWarning' so that it conforms to the canonical names requirements.
读取配置文件的类:
@Component
@RefreshScope
@ConfigurationProperties(prefix = "individualWarning")
public class IndividualWarningProperties {private String title;private String content;public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public String getContent() {return content;}public void setContent(String content) {this.content = content;}
}
报错中Reason已经提示得很清楚了:规范名称应该是串式大小写('-'分隔),小写字母数字字符,并且必须以字母开头
很明显我的@ConfigurationProperties(prefix = "individualWarning")
中的前缀individualWarning
是驼峰的写法,改为横杠分割就行了individual-warning
其实新一点的IDEA已经给出提示了:前缀必须是规范形式
,但工作中使用的是旧版的IDEA没有提示,没能提前发现。
遇到报错不要慌,试着读一读报错原因