【问题标题】:UnrecognizedPropertyException while reading a yaml file读取 yaml 文件时出现 UnrecognizedPropertyException
【发布时间】:2014-04-30 18:55:55
【问题描述】:

在使用 dropwizard 时,

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

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

Config.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"])

在浏览了几个主题之后,我意识到这可能是因为杰克逊无法忽略一些属性。

我尝试了几件事 -

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

2)Jackson how to ignore properties

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

【问题讨论】:

  • 你能贴出你的应用配置类的代码吗?

标签: java web-services yaml dropwizard


【解决方案1】:

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

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

 public DataSourceFactory getDataSourceFactory() {
    return database;
 }

【讨论】:

  • 类似的问题。添加@JSonProperty 修复它。
【解决方案2】:

可能你的配置类没有数据库,你需要在你的配置类中使用这个属性来解析.yml

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-09
    • 1970-01-01
    • 2018-09-24
    • 2012-03-25
    • 2016-10-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多