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

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

Nested configuration by yaml - spring boot

问题

以下是我的YAML配置。

configuration:
  internalUser:
    add:
      city:
        path: path
        name: cityName

      country:
        path: path
        name: countryName

    replace:
      city:
        path: path
        name: cityName
    remove:
      city:
        path: path
        name: cityName
  externalUser:
    add:
      city:
        path: path
        name: cityName

      country:
        path: path
        name: countryName

    replace:
      city:
        path: path
        name: cityName
    remove:
      city:
        path: path
        name: cityName

配置类如下:

@ConfigurationProperties(prefix = "configuration")
public class Configuration {

	private Map<String, Map<String, Map<String, Address>>> internalUser = new HashMap<>();
	 //setter and getter
}

public class Address{
   private String path;
   private String name;
   //setter and getter
}

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

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

英文:

Below is my yaml configuration.

configuration:
  internalUser:
    add:
      city:            
        path: path
        name: cityName

      country:            
        path: path
        name: countryName
                  
    replace:
      city:                       
        path: path
        name: cityName
    remove:
      city:                       
        path: path
        name: cityName
  externalUser:
    add:
      city:            
        path: path
        name: cityName

      country:            
        path: path
        name: countryName
                  
    replace:
      city:                       
        path: path
        name: cityName
    remove:
      city:                       
        path: path
        name: cityName

Configuration class look like:

@ConfigurationProperties(prefix = &quot;configuration&quot;)
public class Configuration {

	private Map&lt;String, Map&lt;String,Map&lt;String&gt;,Address&gt;&gt;&gt; internalUser = new HashMap&lt;&gt;();
	 //setter and getter
}

Public class Address{
	   private String path;
	   private String name;
	   //setter and getter
	}

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

@ConfigurationProperties(prefix = "configuration")
public class Configuration {

    private Map<String, Map<String, Address>> internalUser;
    private Map<String, Map<String, Address>> externalUser;
    // setter and getter
}
英文:

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

First map keys: add, replace, remove

Second map keys: city

@ConfigurationProperties(prefix = &quot;configuration&quot;)
public class Configuration {

    private Map&lt;String, Map&lt;String, Address&gt;&gt;&gt; internalUser;
    private Map&lt;String, Map&lt;String, Address&gt;&gt;&gt; externalUser;
     //setter and getter
}

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:

确定