编程语言
首页 > 编程语言> > java – 使用List of List值解析Yaml时的Jackson异常

java – 使用List of List值解析Yaml时的Jackson异常

作者:互联网

我有一个Yaml文档,其中包含与列表对应的映射条目.这是一个例子:

environments:
  qa: [ 'us-east-1' ]
  staging: [ 'us-east-1', 'us-west-2' ]

我正在使用Jackson 2.3.2使用以下字段将文档解析为一个类.存取:

private Map<String, List<String>> environments = new HashMap<String, List<String>>();

@JsonProperty
public Map<String, List<String>> getEnvironments() {
    return environments;
}

@JsonProperty
public void setEnvironments(Map<String, List<String>> environments) {
    this.environments = environments;
}

我正在使用以下内容测试Yaml文件的读取:

inputStream = new FileInputStream("src/test/resources/configuration_test.yml");
config = mapper.readValue(inputStream, MyConfiguration.class);

使用不特别有用的堆栈跟踪抛出以下异常:

Caused by: java.lang.IllegalArgumentException: Can not find a deserializer for non-concrete Collection type [collection type; class com.google.common.collect.ImmutableList, contains [simple type, class io.dropwizard.metrics.ReporterFactory]]
at com.fasterxml.jackson.databind.deser.BasicDeserializerFactory.createCollectionDeserializer(BasicDeserializerFactory.java:814)
at com.fasterxml.jackson.databind.deser.DeserializerCache._createDeserializer2(DeserializerCache.java:392)
at com.fasterxml.jackson.databind.deser.DeserializerCache._createDeserializer(DeserializerCache.java:350)
at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCache2(DeserializerCache.java:263)
... 42 more

知道如何让杰克逊将Map值解析为List吗?

解决方法:

您需要正确配置ObjectMapper.在dropwizard中,有Jackson.newObjectMapper()

标签:java,yaml,jackson,dropwizard
来源: https://codeday.me/bug/20190717/1490837.html