读取yaml文件时出现UnrecognizedPropertyException

web*_*rer 4 java yaml web-services dropwizard

在使用dropwizard时,

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

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

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
Run Code Online (Sandbox Code Playgroud)

但是,一旦读取文件,我就会收到错误 -

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"])
Run Code Online (Sandbox Code Playgroud)

经过几个主题后,我意识到这可能是因为杰克逊无法忽视一些属性.

我尝试了几件事 -

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

2)杰克逊如何忽视属性

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

Man*_*wam 9

将以下行添加到配置类

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

 public DataSourceFactory getDataSourceFactory() {
    return database;
 }
Run Code Online (Sandbox Code Playgroud)