Getting issue with ConfigurationProperties. I have a configuration like this
com:
test:
app:
units:
unit:
- query:
matchType: "sadas"
- query:
matchType: "sadas"
Wanted to inject this application.yaml to this Map<String,List<Query>>. My Configuration code is like this.
@Component
@ConfigurationProperties(prefix = "com.test.app")
public class UnitsConfiguration {
private Map<String,List<Query>> units;
//settter, getter
}
public class Query {
private String matchType;
//settter, getter
}
Error I am getting while running this.
***************************
APPLICATION FAILED TO START
***************************
Description:
Binding to target [Bindable@7e357c39 type = java.util.List<Query>, value = 'none', annotations = array<Annotation>[[empty]]] failed:
Property: com.test.app.units.unit[0].query.matchtype
Value: "sadas"
Origin: class path resource [application.yaml] - 20:26
Reason: The elements [com.test.app.units.unit[0].query.matchtype,com.test.app.units.unit[1].query.matchtype] were left unbound.
CodePudding user response:
It looks like your YAML is not defined correctly. Here is working YAML along with Java code
Here is formatted YAML
server:
credentials:
- list1:
- key1:
username: root
password: rootpass
- key2:
username: root
password: rootpass
- list2:
- key21:
username: root
password: rootpass
- key22:
username: root
password: rootpass
Here is a Java code
@Component
@ConfigurationProperties(prefix = "server")
public class ServerProperties {
private Map<String, List<Credential>> credentials = new HashMap<>();
//getter and setter
public static class Credential {
private String username;
private String password;
// Getter/Setter
}
}
CodePudding user response:
If you want to have some nested things you need to initalize it and mark them as such. Also add getter/setter to this one:
@NestedConfigurationProperty
private final Map<String,List<Query>> units = new HashMap<>();