0

When unmarshalling a object which has a Instant datatype, I am getting the below exception. Any pointers to resolve the issue?

I am trying to resolve it with java 8 Instant datatype, don't prefer to convert to LocalDate or another datatypes.

Exception in thread "main" java.lang.IllegalArgumentException: com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of java.time.Instant: no suitable constructor found, can not deserialize from Object value (missing default constructor or creator, or perhaps need to add/enable type information?)
 at [Source: [{"transactionType":"ADD_DOC","payload":{"hash":"hashofthedocument12","userId":"123","meta":{"key1":"data1","key2":"data2"}},"consensusTimestamp":{"epochSecond":1522205987,"nano":533000000},"id":"2aaa3863-062d-4fcc-a789-8aad3dfade57"}]; line: 1, column: 148] (through reference chain: java.lang.Object[][0]->ipos.hashgraph.model.ConsensedDocument["consensusTimestamp"])
    at ipos.hashgraph.marshaller.JacksonMarshaller.unmarshalLenient(JacksonMarshaller.java:50)
    at ipos.hashgraph.loadtest.LoadTestor.getTransaction(LoadTestor.java:61)
    at ipos.hashgraph.loadtest.LoadTestor.main(LoadTestor.java:99)
Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of java.time.Instant: no suitable constructor found, can not deserialize from Object value (missing default constructor or creator, or perhaps need to add/enable type information?)
 at [Source: [{"transactionType":"ADD_DOC","payload":{"hash":"hashofthedocument12","userId":"123","meta":{"key1":"data1","key2":"data2"}},"consensusTimestamp":{"epochSecond":1522205987,"nano":533000000},"id":"2aaa3863-062d-4fcc-a789-8aad3dfade57"}]; line: 1, column: 148] (through reference chain: java.lang.Object[][0]->ipos.hashgraph.model.ConsensedDocument["consensusTimestamp"])

Code:

 ObjectMapper mapper = new ObjectMapper();
        mapper.findAndRegisterModules();
        try {
            mapper.readValue(responseEntity1.getBody(), ConsensedDocument[].class);
        } catch (IOException e) {
            e.printStackTrace();
        }

Model Object

String transactionType;
Document payload;
Instant consensusTimestamp;
UUID id;
Minisha
  • 2,117
  • 2
  • 25
  • 56

1 Answers1

4

You need an add-in to support JSR-310 Try following the example below: https://github.com/FasterXML/jackson-modules-java8

Graciano
  • 508
  • 4
  • 11
  • Thanks, after adding the java8 modules. Getting the exception: "com.fasterxml.jackson.databind.JsonMappingException: Expected type float, integer, or string." – Minisha Mar 28 '18 at 04:06
  • Problem seems to me that whatever serialized (wrote) json also needs to register module to produce suitable representation: without module value is taken as POJO and this is not what should happen. So json is basically wrong. – StaxMan Mar 29 '18 at 05:48