gpt4 book ai didi

java - 读取 yaml 文件时发生 UnrecognizedPropertyException

转载 作者:搜寻专家 更新时间:2023-11-01 01:43:06 24 4
gpt4 key购买 nike

在使用 dropwizard 时,

我的 dropwizard 服务读取 config.yml 文件。

public void run() throws Exception {
this.run(new String[] { "server", "src/main/resources/config.yml" });
}

配置.yml 文件:

database:
# the name of your JDBC driver
driverClass: com.mysql.jdbc.Driver

# the username
user: user2connect

# the password
password: password2connect

# the JDBC URL
url: jdbc:mysql://url.to.connect:port

但是,我在读取文件后立即收到错误 -

Exception in thread "main" com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "database" (class com.service.config.DropWizardConfiguration), not marked as ignorable (4 known properties: , "http", "httpConfiguration", "logging", "loggingConfiguration"])
at [Source: N/A; line: -1, column: -1] (through reference chain: com.service.config.DropWizardConfiguration["database"])

在浏览了几个主题之后,我意识到这可能是因为 Jackson 无法忽略少数属性。

我尝试了一些东西 -

1) 添加注解@JsonIgnoreProperty(但不确定是否添加在预期的位置)

2) Jackson how to ignore properties

他们都没有帮助。谁能指出我在这里可能缺少的东西?

最佳答案

将以下行添加到您的配置类

 @Valid
@NotNull
@JsonProperty
private DataSourceFactory database = new DataSourceFactory();

public DataSourceFactory getDataSourceFactory() {
return database;
}

关于java - 读取 yaml 文件时发生 UnrecognizedPropertyException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23395939/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com