Java – unrecognized propertyexception when reading yaml file

When using the dropwizard,

My dropwizard service reads config YML file

public void run() throws Exception {
    this.run(new String[] { "server","src/main/resources/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 once I read the file, I get an error –

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

After a few themes, I realized that this might be because Jackson couldn't ignore some attributes

I tried a few things –

1) Add the comment @ jsonignoreproperty (but I'm not sure if I added it at the expected location)

2)Jackson how to ignore properties

None of them helped Who can point out what I may lack here?

Solution

Add the following line to the configuration class

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

 public DataSourceFactory getDataSourceFactory() {
    return database;
 }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>