2014-04-30 48 views
3

使用dropwizard时,读取yaml文件时发生UnrecognizedPropertyException

我的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

他们没有帮助。任何人都可以指出我可能会在这里失踪吗?

+0

你能后的代码为你的应用程序配置类? – condit

回答

8

添加以下行到配置类

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

public DataSourceFactory getDataSourceFactory() { 
    return database; 
} 
+0

类似的问题。添加@JSonProperty修复了它。 – user1546081

-1

也许你的配置类不具备的数据库,你需要在你的配置类此属性来解析.yml

相关问题