我想知道。我可以这样做或推导它吗?在我的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类,其中包含有关于driverClass,user,password等的getter和注释的字段
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但我可以扩展它。想知道是否可以实现这一点,以便我可以通过这种方法定义更多的数据库配置属性。
或者只是一种更简单的方式?
答案 0 :(得分:2)
您应该能够扩展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; };
}
}