ConfigurationProperties 出現問題。我有這樣的配置
com:
test:
app:
units:
unit:
- query:
matchType: "sadas"
- query:
matchType: "sadas"
想要將此 application.yaml 注入此 Map<String,List<Query>>。我的配置代碼是這樣的。
@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
}
運行此程式時出現錯誤。
***************************
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.
uj5u.com熱心網友回復:
看起來您的 YAML 定義不正確。這是與 Java 代碼一起使用的 YAML
這是格式化的 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
這是一個Java代碼
@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
}
}
uj5u.com熱心網友回復:
如果你想要一些嵌套的東西,你需要初始化它并標記它們。還要向這個添加getter/setter:
@NestedConfigurationProperty
private final Map<String,List<Query>> units = new HashMap<>();
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/518074.html
標籤:爪哇春天弹簧靴
