gpt4 book ai didi

java - 无法构造 java.util.LinkedHashMap : no String-argument constructor/factory 的实例

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:32:21 26 4
gpt4 key购买 nike

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

com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of java.util.LinkedHashMap: no String-argument constructor/factory method to deserialize from String value ('{\"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);
}
}

最佳答案

您的异常准确地告诉您这里发生了什么。

com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of java.util.LinkedHashMap: no String-argument constructor/factory method to deserialize from String value ('{\"hosturl_path\":\"/images\"}')

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

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

如果您要将其调整为您的 TestPojo.class

@JsonProperty("a112b")
private String parameterMap;

您将成功运行。但是,我认为这不是期望的行为。如果您有能力更改您的 sampleFile,Jackson 应该能够正确反序列化它。

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

否则。您将需要指定自定义反序列化器。

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

关于java - 无法构造 java.util.LinkedHashMap : no String-argument constructor/factory 的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51852689/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com