加载了ConfigurationProperties的嵌套映射的键以它们各自的索引为前缀。

huangapple go评论76阅读模式
英文:

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(&quot;oauth2.discovery&quot;)
public class DiscoveryConfigs {

    private Map&lt;String, DiscoveryConfigs.DiscoveryConfig&gt; config;

    public Map&lt;String, DiscoveryConfigs.DiscoveryConfig&gt; getConfig() {
        return this.config;
    }

    public void setConfig(Map&lt;String, DiscoveryConfigs.DiscoveryConfig&gt; discovery) {
        this.config = discovery;
    }

    public static class DiscoveryConfig {
        private String name;
        private Map&lt;String, String&gt; endpoints;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Map&lt;String, String&gt; getEndpoints() {
            return endpoints;
        }

        public void setEndpoints(Map&lt;String, String&gt; 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

huangapple
  • 本文由 发表于 2020年10月21日 23:57:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/64467433.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定