用Jackson反序列化到Map <java.lang.string,java.net.uri =“”>

时间:2016-10-19 13:46:29

标签: java json jackson deserialization uri

我有这个JSON片段:

{
"uploadData": {
    "25967391": {
        "jobid": "jobId",
        "numberOfFiles": "1",
        "outputFiles": {
            "1": "file.pdf"
        },
        "originalFiles": {
            "1": "./target/test-classes/com/tools/myProject/input/File.pdf",
            "2": "./target/test-classes/com/tools/myProject/input/File.csv"
        }
    }
  }
}

我想做的是反序列化原始文件&#34;进入地图。因为&#34; 1&#34;和&#34; 2&#34; in&#34; originalFiles&#34;都是非绝对URI我已经有了自定义类URIStringDeserializer

Paths.get(".").toUri().resolve(uriString).normalize().toASCIIString()

给了我

file:/C:/Users/userName/IdeaProjects/myProject/target/test-classes/com/tools/myProject/input/File.pdf

代表originalFiles的第一个uri。 以下是包含uploadData字段的类:

@JsonIgnoreProperties(ignoreUnknown = true)
public class UploadData{

 @JsonProperty("originalFiles")
 private Map<Integer, URI> originalFiles;

 //getters and setters omitted
}

使用uploadData注释@JsonDeserialize(using = CustomDeserializer.class)字段会产生com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.net.URI out of START_OBJECT token。如何让我的自定义反序列化程序使用tose map?

1 个答案:

答案 0 :(得分:0)

经过一些外部帮助后,找到了解决方案。添加

@JsonDeserializer(contentUsing = CustomDeserializer.class)

做了我想做的事。