Nested configuration by yaml - spring boot

Viewed 241

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 = "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
    }

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 Answers

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

First map keys: add, replace, remove

Second map keys: city

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

    private Map<String, Map<String, Address>>> internalUser;
    private Map<String, Map<String, Address>>> externalUser;
     //setter and getter
}
Related