【问题标题】:how to transforming yaml file to a list with complex object generic如何将 yaml 文件转换为具有复杂对象泛型的列表
【发布时间】:2018-12-31 14:04:30
【问题描述】:

我的 yaml 是这样的:

- code: code1
  type: REST
  base-url: https://api.test1.com/api/v1
  endPoints:
    - type: BATCH
      path: /transmissions
      method: POST
      content-type: application/json
      headers: [{key: Authorization, expression: '#root.apiKey'}]
      dependencies: []
      params:
        - key: campaign_id
          source: campaign_id
        - key: subject
          source: subject
- code: code2
  type: SAAS
  base-url: https://api.test2.com/api/v1
  endPoints:
    - type: ONE
      path: /transmissions2
      method: GET
      content-type: application/json
      headers: [{key: Authorization, expression: '#root.apiKey'}]
      dependencies: []
      params:
        - key: campaign_id
          source: campaign_id
        - key: subject
          source: subject
#
...

java 泛型类:

@Data
public class MappingField {
    private String code;
    private ServiceType type;
    @JsonProperty("base-url")
    private String baseUrl;
    private int port;
    private List<EndPoint> endPoints;
    private Map<String, EndPoint> endPointMapByType = new HashMap<>();

    @Data
    public static class EndPoint {
        private EndpointType type;
        private String path;
        @JsonProperty("content-type")
        private String contentType;
        private HttpMethod method;
        @JsonProperty("substitution_data")
        private List<String> substitutionData;
        private List<Map<String,Object>> headers;
        private List<Map<String,Object>> params;
        private Map<String,Object> response;
    }
}

我使用snakeYaml解析这个文件,像这样:见https://bitbucket.org/asomov/snakeyaml/wiki/Documentation#markdown-header-type-safe-collections

@Test
public void test4(){
    Constructor constructor = new Constructor(List.class);//Car.class is root
    TypeDescription carDescription = new TypeDescription(List.class);
   // carDescription.putListPropertyType("null",MappingField.class);
    constructor.addTypeDescription(carDescription);
    Yaml yaml = new Yaml(constructor);
    final Object load = 
    yaml.load(this.getClass().getClassLoader().getResourceAsStream("test1.yml"));

    assertNotNull("load");
}

当它运行时,它返回List&lt;Object&gt;,我会做些什么让它返回List&lt;MappingField&gt;? 如何将java泛型“MappingFields”添加到列表集合中?

注意:我的 yaml 根目录是一个列表

【问题讨论】:

  • 你试过了吗? final List load = yaml.load(this.getClass().getClassLoader().getResourceAsStream("test1.yml"));
  • 我试过这个,它可以返回 LIst,像这样:final List load = (List&lt;MappingField&gt;) yaml.load(this.getClass().getClassLoader().getResourceAsStream("test.yml"));

标签: spring yaml snakeyaml


【解决方案1】:
final List<MappingFields> load = 
(List<MappingFields>) yaml.load(this.getClass().getClassLoader().getResourceAsStream("test1.yml"));

如果你在 YAML 中添加一个额外的标签就会起作用

- !!your.package.MappingFields
  code: code1
...

【讨论】:

    猜你喜欢
    • 2013-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-08
    • 1970-01-01
    • 1970-01-01
    • 2021-11-06
    相关资源
    最近更新 更多