读取Yaml文件时出现UnrecognizedPropertyException [英] UnrecognizedPropertyException while reading a yaml file

查看:133
本文介绍了读取Yaml文件时出现UnrecognizedPropertyException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用dropwizard时,

While working with dropwizard,

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

My dropwizard service reads the config.yml file.

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

Config.yml文件:

Config.yml file :

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

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

But, I am getting an error as soon as the file is read -

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无法忽略一些属性而引起的。

After going through few topics, I realized that this might be causing because of Jackson is not able to ignore few properties.

我尝试了几件事-

1)添加了注释@JsonIgnoreProperty(但不确定是否添加了它)

1) Added annotation @JsonIgnoreProperty (But not sure if I added it at expected place)

2)杰克逊如何忽略属性

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

None of them helped. Can anyone point me what I might be missing here?

推荐答案

将以下行添加到配置类中

add the following lines to your configuration class

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

 public DataSourceFactory getDataSourceFactory() {
    return database;
 }

这篇关于读取Yaml文件时出现UnrecognizedPropertyException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆