gpt4 book ai didi

java - Jackson ObjectMapper 无法反序列化 POJO,抛出异常 : No suitable constructor found for type [. ..]:无法从 JSON 对象实例化

转载 作者:IT老高 更新时间:2023-10-28 12:54:19 30 4
gpt4 key购买 nike

我已尝试测试以下代码,但没有成功:

class TestClass
{
private class ND2Customer
{
public String name;
public String description;
public String email;
public Boolean multiuser;

public String dnszone;
public String uri;
public String type;

public ND2Customer()
{

}
}

@Test
public void TestJackson() throws JsonParseException, JsonMappingException, IOException
{
String json="{\"description\": \"test1u\", \"dnszone\": \"test1.public.sevenltest.example.com.\", \"uri\": \"http://199.127.129.69/customer/test1\", \"multiuser\": true, \"type\": \"2.0.3-3146\", \"email\": \"test1@com.com\", \"name\": \"test1\"}";
ObjectMapper mapper = new ObjectMapper();

ND2Customer casted=mapper.readValue(json, ND2Customer.class);

String castedback=mapper.defaultPrettyPrintingWriter().writeValueAsString(casted);
System.out.println(castedback);
}
}

这个问题与这个不同: Deserializing JSON with Jackson - Why JsonMappingException "No suitable constructor"?

还有这个: JsonMappingException: No suitable constructor found for type [simple type, class ]: can not instantiate from JSON object

还有这个: JsonMappingException: No suitable constructor found for type [simple type, class ]: can not instantiate from JSON object

因为我手动覆盖了默认构造函数,它不是子类。

我该如何解决这个问题?

最佳答案

使其成为静态的。 Jackson 无法反序列化为内部类

关于java - Jackson ObjectMapper 无法反序列化 POJO,抛出异常 : No suitable constructor found for type [. ..]:无法从 JSON 对象实例化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12916774/

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