Jackson ObjectMapper无法反序列化POJO,抛出异常:没有为类型[...]找到合适的构造函数:无法从JSON对象实例化 [英] Jackson ObjectMapper cannot deserialize POJO, throws an exception: No suitable constructor found for type [...]: can not instantiate from JSON object

查看:467
本文介绍了Jackson ObjectMapper无法反序列化POJO,抛出异常:没有为类型[...]找到合适的构造函数:无法从JSON对象实例化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have tried to test the following code with no success:

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

此问题与此不同:
使用Jackson反序列化JSON - 为什么选择JsonMappingException没有合适的构造函数 ?

这一个:
JsonMappingException:找不到类型[simple type,class]的合适构造函数:无法从JSON对象实例化

和这一个:
JsonMappingException:找不到类型[simple type,class]的合适构造函数:无法从JSON对象实例化

因为我手动覆盖默认构造函数,而不是一个子类。

as I have manually override the default constructor, and its not a subclass.

如何解决这个问题?

推荐答案

让它静止。杰克逊不能反序列化到内部类

Make it static. Jackson can not deserialize to inner classes

这篇关于Jackson ObjectMapper无法反序列化POJO,抛出异常:没有为类型[...]找到合适的构造函数:无法从JSON对象实例化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆