[英]property in subclass of DatabaseConfiguration can not be found in Dropwizard yaml (hierarchical issue)
I want to know. 我想知道。 Can i do this or a derivation of it? 我可以做到这一点吗? In my Dropwizard config.yml I wanted to extend databaseConfiguration class so to add another property to the config.yml as below 在我的Dropwizard config.yml中,我想扩展databaseConfiguration类,以便向config.yml添加另一个属性,如下所示
config.yml config.yml
database
driverClass: org.postgresql.Driver
user: user1
password: password
exampleProperty: property1 (Tried a few other permutations)
Assume theres a DatabaseConfiguration class that has fields with getters and annotations for driverClass, user, password etc 假设有一个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; };
}
The main class Server.java passes the ExtendedDatabaseConfiguration to the Hibernate Bundle via the call (formerly passed the DatabaseConfiguration) 主类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);
This is all in order for CustomHibernateBundle to then use this field as it pleases. 这一切都是为了让CustomHibernateBundle然后根据需要使用此字段。
Whatever i tried either i got the error jackson.databind.exc.UnrecognizedPropertyException or i get the following 无论我尝试了什么,我都会收到错误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)
Just to note. 只是要注意。 I have no access to modify DatabaseConfiguration but i can extend it. 我无权修改DatabaseConfiguration,但可以对其进行扩展。 Wondering whether this can be achieved so that i can define more database configuration properties via this methodology. 想知道是否可以实现这一点,以便我可以通过这种方法定义更多的数据库配置属性。
Or is there just a simpler way? 还是有更简单的方法?
You should be able to extend DatabaseConfiguration - I think you just added an extra configuration field that the parser is now expecting (although I'm not sure where the warning about the missing key
is coming from). 您应该能够扩展DatabaseConfiguration-我认为您刚刚添加了解析器现在期望的额外配置字段(尽管我不确定有关丢失key
的警告来自何处)。 Try the following configuration class: 尝试以下配置类:
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.