无法构造java.util.LinkedHashMap的实例:没有字符串参数构造函数/工厂

时间:2018-08-15 03:49:20

标签: java jackson objectmapper

解析文件并转换为POJO时遇到的问题。 在我得到的异常之下。

  

com.fasterxml.jackson.databind.JsonMappingException:无法构造java.util.LinkedHashMap的实例:否要从字符串值反序列化的字符串参数构造函数/工厂方法('{\“ hosturl_path \”:\“ / images \“}')

示例json文件:

{"test": [{
     "a112a": "testhost", 
     "a112b": "{\"hosturl_path\":\"/images\"}"
}]}

POJO-

import java.io.Serializable;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY, getterVisibility = JsonAutoDetect.Visibility.ANY,
        setterVisibility = JsonAutoDetect.Visibility.ANY)
public class TestPojo implements Serializable
{
    private static final long serialVersionUID = 638312139361412L;

    @JsonProperty("a112a")
    private String host;

    @JsonProperty("a112b")
    private Map<String,String> parameterMap;

    public TestPojo()
    {
    }

    public TestPojo(String host, Map<String, String> parameterMap)
    {
        this.host = host;
        this.parameterMap = parameterMap;
    }

    public String getHost()
    {
        return host;
    }

    public void setHost(String host)
    {
        this.host = host;
    }

    public Map<String, String> getParameterMap()
    {
        return parameterMap;
    }

    public void setParameterMap(Map<String, String> parameterMap)
    {
        this.parameterMap = parameterMap;
    }
}

主班

import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;


public class TestApp
{
    public static void main(String[] args) throws IOException
    {
        ObjectMapper mapper = new ObjectMapper();

        String fileName = "sampleFile.json";

        URL resource = TestApp.class.getResource(fileName);

        Map<String,List<TestPojo>> map = mapper.readValue(resource, new
                TypeReference<Map<String, List<TestPojo>>>()
                {
                });

        System.out.println(map);
    }
}

1 个答案:

答案 0 :(得分:2)

您的例外情况是告诉您这里到底发生了什么。

  

com.fasterxml.jackson.databind.JsonMappingException:无法构造java.util.LinkedHashMap的实例:否要从字符串值反序列化的字符串参数构造函数/工厂方法('{\“ hosturl_path \”:\“ / images \“}')

在您的TestPojo.class中,您试图将String反序列化为Map

@JsonProperty("a112b")
private Map<String,String> parameterMap;

如果要根据您的TestPojo.class进行调整

@JsonProperty("a112b")
private String parameterMap;

您将成功运行。但是,我想这不是理想的行为。如果您有能力更改自己的sampleFile杰克逊应该可以正确地反序列化。

{"test": [{
 "a112a": "testhost", 
 "a112b": {"hosturl_path":"images"}
}]}

否则。您将需要指定自定义解串器。

@JsonDeserialize(CustomParameterDeserializer.class)
@JsonProperty("a112b")
private Map<String,String> parameterMap;