java - 读取 yaml 文件时发生 UnrecognizedPropertyException

标签 java web-services yaml dropwizard

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

相关文章:

java - 从 Web 应用程序中现有的 SOAP Web 服务迁移到 REST Web 服务

web-services - 我可以使用 PowerShell 的 New-WebServiceProxy 过滤基于 WSDL 的 SOAP 查询以仅发送一个参数吗?

azure-devops - Azure DevOps 多阶段管道等待批准

json - 我可以/应该使用 YAML 作为 RESTful Web 服务中的有效负载吗?

java - 为包指定 maven 依赖项

java - 具有相同字符串值的两个不同变量引用相同的内存地址

java - JAX-WS 部署

yaml - 如何在 Jekyll/Liquid 中获取哈希的键和值?

java - PrimeFaces 3.5 和 DataTables(多选)

java - 在java中获取AppData路径时URI语法错误