英文:
Keys of nested map loaded with ConfigurationProperties are prefixed with their respective indexes
问题
使用Spring Boot,我试图使用注解 @ConfigurationProperties("oauth2.discovery") 加载配置。
一切工作正常,除了一个问题:嵌套映射的键不是根据配置文件内容加载的,而是以它们各自的索引作为前缀。
以下是用于加载配置的代码:
@ConfigurationProperties("oauth2.discovery")
public class DiscoveryConfigs {
private Map<String, DiscoveryConfigs.DiscoveryConfig> config;
public Map<String, DiscoveryConfigs.DiscoveryConfig> getConfig() {
return this.config;
}
public void setConfig(Map<String, DiscoveryConfigs.DiscoveryConfig> discovery) {
this.config = discovery;
}
public static class DiscoveryConfig {
private String name;
private Map<String, String> endpoints;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Map<String, String> getEndpoints() {
return endpoints;
}
public void setEndpoints(Map<String, String> endpoints) {
this.endpoints = endpoints;
}
}
}
这是我的 application.yml 配置文件:
oauth2:
discovery:
config:
affiliation:
name: affiliation-service
endpoints:
public: /getPublic
forRoleA: /getForRoleA
forRoleB: /getForRoleB
blabla:
name: ...
使用 DiscoveryConfigs 加载 application.yml 后,我得到以下内容:
(debugger view in IntelliJ)
endpoints = {LinkedHashMap@10957} size >= 3
"0.public" -> "/getPublic"
"1.forRoleA" -> "/getForRoleA"
"2.forRoleB" -> "/getForRoleB"
其中,键的示例是 "0.public",而不是 "public"。
所以我确定我漏掉了什么,但我无法弄清楚是什么...
英文:
With spring boot I'm trying to load configuration using the annotation @ConfigurationProperties("oauth2.discovery").
Everything works fine except the fact that keys of a nested map are not loaded with the configuration file content but instead they are prefixed with their respectiveindexes.
This is the code used to load the configuration :
@ConfigurationProperties("oauth2.discovery")
public class DiscoveryConfigs {
private Map<String, DiscoveryConfigs.DiscoveryConfig> config;
public Map<String, DiscoveryConfigs.DiscoveryConfig> getConfig() {
return this.config;
}
public void setConfig(Map<String, DiscoveryConfigs.DiscoveryConfig> discovery) {
this.config = discovery;
}
public static class DiscoveryConfig {
private String name;
private Map<String, String> endpoints;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Map<String, String> getEndpoints() {
return endpoints;
}
public void setEndpoints(Map<String, String> endpoints) {
this.endpoints = endpoints;
}
}
}
This is my application.yml
oauth2:
discovery:
config:
affiliation:
name: affiliation-service
endpoints:
- public: /getPublic
- forRoleA: /getForRoleA
- forRoleB: /getForRoleB
blabla:
name: ...
And using DiscoveryConfigs to load the application.yml I got the following content:
> ( debugger view in IntellIj )
> endpoints = {LinkedHashMap@10957} size > = 3
> "0.public" -> "/getPublic"
> "1.forRoleA" -> "/getForRoleA"
> "2.forRoleB" -> "/getForRoleB"
Where keys are for example "0.public" instead of "public"
So I'm sure I missed something but I cannot figure what...
答案1
得分: 2
查看关于 Binding Maps 的说明在 spring.io 上。以下内容应该有效:
oauth2:
discovery:
config:
affiliation:
name: affiliation-service
endpoints:
public: /getPublic
forRoleA: /getForRoleA
forRoleB: /getForRoleB
英文:
Checkout the description to Binding Maps on spring.io. The following should work:
oauth2:
discovery:
config:
affiliation:
name: affiliation-service
endpoints:
public: /getPublic
forRoleA: /getForRoleA
forRoleB: /getForRoleB
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论