Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jackson ObjectMapper cannot deserialize POJO, throws an exception: No suitable constructor found for type [...]: can not instantiate from JSON object

Tags:

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\": \"[email protected]\", \"name\": \"test1\"}";     ObjectMapper mapper = new ObjectMapper();      ND2Customer casted=mapper.readValue(json, ND2Customer.class);      String castedback=mapper.defaultPrettyPrintingWriter().writeValueAsString(casted);     System.out.println(castedback);   }  } 

This problem is different from this one: Deserializing JSON with Jackson - Why JsonMappingException "No suitable constructor"?

and this one: JsonMappingException: No suitable constructor found for type [simple type, class ]: can not instantiate from JSON object

and this one: JsonMappingException: No suitable constructor found for type [simple type, class ]: can not instantiate from JSON object

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

How do I fix this problem?

like image 216
tribbloid Avatar asked Oct 16 '12 14:10

tribbloid


People also ask

What is ObjectMapper class in Jackson?

ObjectMapper is the main actor class of Jackson library. ObjectMapper class ObjectMapper provides functionality for reading and writing JSON, either to and from basic POJOs (Plain Old Java Objects), or to and from a general-purpose JSON Tree Model (JsonNode), as well as related functionality for performing conversions.

Does Jackson need default constructor?

Jackson uses default (no argument) constructor to create object and then sets value using setters. so you only need @NoArgsConstructor and @Setter.

What is the use of ObjectMapper in spring boot?

Overview. When using JSON format, Spring Boot will use an ObjectMapper instance to serialize responses and deserialize requests. In this tutorial, we'll take a look at the most common ways to configure the serialization and deserialization options. To learn more about Jackson, be sure to check out our Jackson tutorial.

Should you inject ObjectMapper?

Yes, that is safe and recommended.


2 Answers

Make it static. Jackson can not deserialize to inner classes

like image 151
eugen Avatar answered Sep 22 '22 22:09

eugen


The problem is probably that Jackson can't properly reach your ND2Customer class to invoke its constructor because it is private, as your class otherwise looks just fine. Try making it public and seeing if that works.

like image 27
Donal Fellows Avatar answered Sep 26 '22 22:09

Donal Fellows