如何在春季启动时通过YAML配置创建地图?

时间:2020-01-05 11:37:54

标签: java spring spring-boot yaml

我想在春季启动时创建Map类型的地图,下面是我在application.yml和相关的Java类中配置的内容

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

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

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

但地图未创建,我希望地图会在其中包含以下数据的情况下创建

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

对此有任何帮助吗?

3 个答案:

答案 0 :(得分:1)

感谢所有试图帮助我解决问题的人,我在build.gradle中找到了该插件的解决方案

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

现在工作正常。

答案 1 :(得分:-1)

您必须在具有属性名称的YAML配置中创建Arraylist。比仅通过调用属性即可调用对象的本质。

示例:

    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));

答案 2 :(得分:-3)

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