嵌套配置通过YAML实现 – Spring Boot

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

Nested configuration by yaml - spring boot

问题

以下是我的YAML配置。

  1. configuration:
  2. internalUser:
  3. add:
  4. city:
  5. path: path
  6. name: cityName
  7. country:
  8. path: path
  9. name: countryName
  10. replace:
  11. city:
  12. path: path
  13. name: cityName
  14. remove:
  15. city:
  16. path: path
  17. name: cityName
  18. externalUser:
  19. add:
  20. city:
  21. path: path
  22. name: cityName
  23. country:
  24. path: path
  25. name: countryName
  26. replace:
  27. city:
  28. path: path
  29. name: cityName
  30. remove:
  31. city:
  32. path: path
  33. name: cityName

配置类如下:

  1. @ConfigurationProperties(prefix = "configuration")
  2. public class Configuration {
  3. private Map<String, Map<String, Map<String, Address>>> internalUser = new HashMap<>();
  4. //setter and getter
  5. }
  6. public class Address{
  7. private String path;
  8. private String name;
  9. //setter and getter
  10. }

在加载应用程序时,它失败并且无法转换对象。

我的配置有什么问题吗?或者我们可以为这个配置使用嵌套配置吗?
请在配置方面帮助我。

英文:

Below is my yaml configuration.

  1. configuration:
  2. internalUser:
  3. add:
  4. city:
  5. path: path
  6. name: cityName
  7. country:
  8. path: path
  9. name: countryName
  10. replace:
  11. city:
  12. path: path
  13. name: cityName
  14. remove:
  15. city:
  16. path: path
  17. name: cityName
  18. externalUser:
  19. add:
  20. city:
  21. path: path
  22. name: cityName
  23. country:
  24. path: path
  25. name: countryName
  26. replace:
  27. city:
  28. path: path
  29. name: cityName
  30. remove:
  31. city:
  32. path: path
  33. name: cityName

Configuration class look like:

  1. @ConfigurationProperties(prefix = &quot;configuration&quot;)
  2. public class Configuration {
  3. private Map&lt;String, Map&lt;String,Map&lt;String&gt;,Address&gt;&gt;&gt; internalUser = new HashMap&lt;&gt;();
  4. //setter and getter
  5. }
  6. Public class Address{
  7. private String path;
  8. private String name;
  9. //setter and getter
  10. }

While loading the application it is failing and not able to cast the object.

Is there anything is wrong with my configuration? Or can We use the nested configuration for this configuration?
Please help me in the configuration.

答案1

得分: 3

你有一个多余的地图。你只需要两个地图。

第一个地图的键:add,replace,remove

第二个地图的键:city

  1. @ConfigurationProperties(prefix = "configuration")
  2. public class Configuration {
  3. private Map<String, Map<String, Address>> internalUser;
  4. private Map<String, Map<String, Address>> externalUser;
  5. // setter and getter
  6. }
英文:

You have one too many maps. You only need two maps.

First map keys: add, replace, remove

Second map keys: city

  1. @ConfigurationProperties(prefix = &quot;configuration&quot;)
  2. public class Configuration {
  3. private Map&lt;String, Map&lt;String, Address&gt;&gt;&gt; internalUser;
  4. private Map&lt;String, Map&lt;String, Address&gt;&gt;&gt; externalUser;
  5. //setter and getter
  6. }

huangapple
  • 本文由 发表于 2020年8月31日 22:32:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/63672803.html
匿名

发表评论

匿名网友

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

确定