当前位置: 首页 > 面试题库 >

Jackson ObjectMapper无法反序列化POJO,引发异常:找不到适合类型[…]的合适构造函数:无法从JSON对象实例化

金阳华
2023-03-14
问题内容

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

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);
  } 
}

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

我该如何解决这个问题?


问题答案:

使其静态。杰克逊无法反序列化内部阶级



 类似资料: