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

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

Keys of nested map loaded with ConfigurationProperties are prefixed with their respective indexes

问题

使用Spring Boot,我试图使用注解 @ConfigurationProperties("oauth2.discovery") 加载配置。

一切工作正常,除了一个问题:嵌套映射的键不是根据配置文件内容加载的,而是以它们各自的索引作为前缀。

以下是用于加载配置的代码:

  1. @ConfigurationProperties("oauth2.discovery")
  2. public class DiscoveryConfigs {
  3. private Map<String, DiscoveryConfigs.DiscoveryConfig> config;
  4. public Map<String, DiscoveryConfigs.DiscoveryConfig> getConfig() {
  5. return this.config;
  6. }
  7. public void setConfig(Map<String, DiscoveryConfigs.DiscoveryConfig> discovery) {
  8. this.config = discovery;
  9. }
  10. public static class DiscoveryConfig {
  11. private String name;
  12. private Map<String, String> endpoints;
  13. public String getName() {
  14. return name;
  15. }
  16. public void setName(String name) {
  17. this.name = name;
  18. }
  19. public Map<String, String> getEndpoints() {
  20. return endpoints;
  21. }
  22. public void setEndpoints(Map<String, String> endpoints) {
  23. this.endpoints = endpoints;
  24. }
  25. }
  26. }

这是我的 application.yml 配置文件:

  1. oauth2:
  2. discovery:
  3. config:
  4. affiliation:
  5. name: affiliation-service
  6. endpoints:
  7. public: /getPublic
  8. forRoleA: /getForRoleA
  9. forRoleB: /getForRoleB
  10. blabla:
  11. name: ...

使用 DiscoveryConfigs 加载 application.yml 后,我得到以下内容:

  1. (debugger view in IntelliJ)
  2. endpoints = {LinkedHashMap@10957} size >= 3
  3. "0.public" -> "/getPublic"
  4. "1.forRoleA" -> "/getForRoleA"
  5. "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 :

  1. @ConfigurationProperties(&quot;oauth2.discovery&quot;)
  2. public class DiscoveryConfigs {
  3. private Map&lt;String, DiscoveryConfigs.DiscoveryConfig&gt; config;
  4. public Map&lt;String, DiscoveryConfigs.DiscoveryConfig&gt; getConfig() {
  5. return this.config;
  6. }
  7. public void setConfig(Map&lt;String, DiscoveryConfigs.DiscoveryConfig&gt; discovery) {
  8. this.config = discovery;
  9. }
  10. public static class DiscoveryConfig {
  11. private String name;
  12. private Map&lt;String, String&gt; endpoints;
  13. public String getName() {
  14. return name;
  15. }
  16. public void setName(String name) {
  17. this.name = name;
  18. }
  19. public Map&lt;String, String&gt; getEndpoints() {
  20. return endpoints;
  21. }
  22. public void setEndpoints(Map&lt;String, String&gt; endpoints) {
  23. this.endpoints = endpoints;
  24. }
  25. }
  26. }

This is my application.yml

  1. oauth2:
  2. discovery:
  3. config:
  4. affiliation:
  5. name: affiliation-service
  6. endpoints:
  7. - public: /getPublic
  8. - forRoleA: /getForRoleA
  9. - forRoleB: /getForRoleB
  10. blabla:
  11. 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 上。以下内容应该有效:

  1. oauth2:
  2. discovery:
  3. config:
  4. affiliation:
  5. name: affiliation-service
  6. endpoints:
  7. public: /getPublic
  8. forRoleA: /getForRoleA
  9. forRoleB: /getForRoleB
英文:

Checkout the description to Binding Maps on spring.io. The following should work:

  1. oauth2:
  2. discovery:
  3. config:
  4. affiliation:
  5. name: affiliation-service
  6. endpoints:
  7. public: /getPublic
  8. forRoleA: /getForRoleA
  9. 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:

确定