简体   繁体   中英

How to create a Map from YAML configuation in spring boot?

I want to create a map of type Map> in spring boot , below is the thing i configured in my application.yml and related java class

labels:
  nodetypes:
    payment:
      - customerId
      - emailId
      - movileNumber
    profile:
    loyality:

@Data
@ConfigurationProperties(prefix = "labels")
@Component
public class NodeTypeToResponseProps {

    Map<String, List<String>> nodetypes = new HashMap<>();
}

but map is not creating , i am expecting , a map will get created with below data in it

{payment : [customerId,emailId,movileNumber] ,profile:[] ,loyality:[] } 

any help on this please ?

Thanks to everyone , who tried to help me in solving the issue , i found the solution for this plugin in my build.gradle

id 'io.freefair.lombok' version '3.8.4'

it is working fine now .

You have to create Arraylists int this YAML configuration which has the name of the attributes. Than your essential able to call your Object by only calling the attribute.

Example:

    YamlConfiguration yaml = new YamlConfiguration();
    HashMap<String, List<String>> nodetypes = new HashMap<>();
    //setter
    for(String key :nodetypes.keySet()) 
        yaml.set("path."+key,  nodetypes.get(key));
        yaml.set("path."+new String("keys"), nodetypes.keySet());



    //getter
    HashMap<String, List<String>> cp = new HashMap<>();

    for(String key:yaml.getStringList("path."+new String("keys")))
        cp.put(key, yaml.getStringList("path."+key));

您可以尝试将@Component放在@ConrigurationProerties之前

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM