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 – 在Heroku上运行Dropwizard应用程序:R10无法绑定到$PORT


下一篇:mysql – 如果存在表,则dropwizard hibernate liquibase迁移失败