[英]Annotations when parsing YAML file to Configuration subclass in Dropwizard
[英]property in subclass of DatabaseConfiguration can not be found in Dropwizard yaml (hierarchical issue)
我想知道。 我可以做到這一點嗎? 在我的Dropwizard config.yml中,我想擴展databaseConfiguration類,以便向config.yml添加另一個屬性,如下所示
config.yml
database
driverClass: org.postgresql.Driver
user: user1
password: password
exampleProperty: property1 (Tried a few other permutations)
假設有一個DatabaseConfiguration類,該類具有帶有getter的字段以及driverClass,用戶,密碼等的注釋
public class AppConfiguration extends Configuration {
@Valid
@NotNull
@JsonProperty
private DatabaseConfiguration database = new DatabaseConfiguration();
public DatabaseConfiguration getDatabaseConfiguration() {
return database;
}
@Valid
@NotNull
@JsonProperty
private ExtendedDatabaseConfiguration extDatabase;
public ExtendedDatabaseConfiguration getExtendedDatabaseConfiguration() {
return extDatabase;
}
public class ExtendedDatabaseConfiguration extends DatabaseConfiguration {
@Valid
@NotNull
@JsonProperty
private String exampleProperty;
public String getExampleProperty() { return exampleProperty; };
}
主類Server.java通過調用將ExtendedDatabaseConfiguration傳遞給Hibernate Bundle(以前傳遞給DatabaseConfiguration)
private final CustomHibernateBundle<AppConfiguration> hibernateBundle = new CustomHibernateBundle<AppConfiguration>() {
@Override
//Giving the subclass holding the new database property to the CustomHibernateBundle
public DatabaseConfiguration getDatabaseConfiguration(AppConfiguration configuration) {
return configuration.getExtendedDatabaseConfiguration();
}
};
SimpleModule simpleModule = new SimpleModule();
simpleModule.addAbstractTypeMapping(DatabaseConfiguration.class, ExtendedDatabaseConfiguration.class);
bootstrap.getObjectMapperFactory().registerModule(simpleModule);
這一切都是為了讓CustomHibernateBundle然后根據需要使用此字段。
無論我嘗試了什么,我都會收到錯誤jackson.databind.exc.UnrecognizedPropertyException或得到以下內容
Exception in thread "main" com.yammer.dropwizard.config.ConfigurationException: powaaim.yml has the following errors:
* extDatabase may not be null (was null)
* exampleProperty may not be null (was null)
只是要注意。 我無權修改DatabaseConfiguration,但可以對其進行擴展。 想知道是否可以實現這一點,以便我可以通過這種方法定義更多的數據庫配置屬性。
還是有更簡單的方法?
您應該能夠擴展DatabaseConfiguration-我認為您剛剛添加了解析器現在期望的額外配置字段(盡管我不確定有關丟失key
的警告來自何處)。 嘗試以下配置類:
public class AppConfiguration extends Configuration {
@Valid
@NotNull
@JsonProperty
private ExtendedDatabaseConfiguration database = new ExtendedDatabaseConfiguration();
public ExtendedDatabaseConfiguration getDatabaseConfiguration() {
return database;
}
static class ExtendedDatabaseConfiguration extends DatabaseConfiguration {
@Valid
@NotNull
@JsonProperty
private String exampleProperty;
public String getExampleProperty() { return exampleProperty; };
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.