Reputation: 2832
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
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
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)
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.
Whatever i tried either i got the error jackson.databind.exc.UnrecognizedPropertyException or i get the following
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. 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?
Upvotes: 1
Views: 1617
Reputation: 10962
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). 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; };
}
}
Upvotes: 2