> 在用于 Java Dropwizard 的 Yaml 中,java,jackson,yaml,dropwizard"/>

首页 > 解决方案 > 是否可以解析地图> 在用于 Java Dropwizard 的 Yaml 中

问题描述

我有一个问题,我试图使用 a 解析 yaml 文件,Map<String, Pin>但 Dropwizard 一直抱怨它无法解析它。

我的 Yaml 文件如下所示 -

us:
  baseUrl: https://www.example.com
  fixture1: https://www.example.com/fixture1
  fixture2: https://www.example.com/fixture2
  filters:
    pins:
      fixture1:
        - category: test_category1
            urls:
                - url: https://www.example.com/fixture1/test
                  name: fixture1 entry
                  weight: 1000

      fixture2:
        - category: test_category_2
            urls:
                - url: https://www.example.com/fixture2/test
                  name: fixture2 entry
                  weight: 1000

我使用杰克逊用代码解析它

    try {
        Map<String, Country> countries = mapper.readValue(getClass().getResourceAsStream(COUNTRY_CONFIG_FILE),
                new TypeReference<Map<String, Country>>() {
                });
    } catch (IOException e) {
        throw new CrossLinkerException("Country config is null");
    }

Country 对象是一个 pojo,具有以下属性

public class Country {
    private String baseUrl;
    private String goodsUrl;
    private String localUrl;
    private TestFilter filters;
}
class TestFilter {
    private Map<String, List<Pin>> pins;
}
public class Pin {
    private String category;
    private List<Link> urls;
}
public class Link {
    private String url;
    private String name;
    private Float weight;
}

当我启动应用程序时,它失败并出现错误 Country config is null。

标签: javajacksonyamldropwizard

解决方案


推荐阅读