Vaibs
Vaibs

Reputation: 1606

Nested configuration by yaml - spring boot

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.

Upvotes: 2

Views: 746

Answers (1)

George
George

Reputation: 3020

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
}

Upvotes: 3

Related Questions