Jackson ObjectMapper无法反序列化POJO,抛出异常:没有为类型[...]找到合适的构造函数:无法从JSON对象实例化

时间:2012-10-16 14:19:19

标签: java json spring jackson

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

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

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

如何解决此问题?

2 个答案:

答案 0 :(得分:59)

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

答案 1 :(得分:0)

问题可能是Jackson无法正确到达你的ND2Customer类来调用它的构造函数,因为它是private,因为你的类看起来很好。尝试制作public并查看是否有效。