读取yaml文件时出现UnrecognizedPropertyException

时间:2014-04-30 18:55:56

标签: java web-services yaml dropwizard

使用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

他们都没有帮助。谁能指出我在这里可能会缺少什么?

2 个答案:

答案 0 :(得分:9)

将以下行添加到配置类

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

 public DataSourceFactory getDataSourceFactory() {
    return database;
 }

答案 1 :(得分:-1)

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