简体   繁体   中英

Jackson date deserialize error from JSON file

I am trying to deserialize JSON file to POJO object , but getting an error .

JSON file looks like (NotificationPOST.json)

  {
   "endDate":"2018-12-27",
   "malfunctionStartDate":"2018-11-20T22:22:22",
   "malfunctionEndDate":"2018-11-21T12:34:46"
   }

POJO Class has

 @DateFormat(field = "Start Date", groups = Order.Level2.class)
    @JsonSerialize(using = DateSerializer.class)
    private DateTime startDate;

    @DateFormat(field = "End Date", groups = Order.Level2.class)
    @JsonSerialize(using = DateSerializer.class)
    private DateTime endDate;

Custom DateSerializer class

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import java.io.IOException;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

public class DateSerializer extends JsonSerializer<DateTime> {
    private static DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd");

    public DateSerializer() {
    }

    public void serialize(DateTime value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException {
        gen.writeString(formatter.print(value));
    }
}

My test class by which I am trying to run

@BeforeClass
    public static void init() {
        notificationPOST = new File("src/test/resources/json/NotificationPOST.json");
    }

@Test
public void notificationPayloadToEntityTest () throws IOException {
private ObjectMapper objectMapper = new ObjectMapper();

    NotificationPOST post = objectMapper.readValue(notificationPOST, NotificationPOST.class);
}

The error which I am getting

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `org.joda.time.DateTime` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('2018-12-27')
 at [Source: (File); line: 13, column: 14] (through reference chain: com.sap.iot.ain.notification.payload.NotificationPOST["endDate"])

    at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:63)
    at com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1342)
    at com.fasterxml.jackson.databind.DeserializationContext.handleMissingInstantiator(DeserializationContext.java:1031)
    at com.fasterxml.jackson.databind.deser.ValueInstantiator._createFromStringFallbacks(ValueInstantiator.java:371)
    at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator.createFromString(StdValueInstantiator.java:323)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromString(BeanDeserializerBase.java:1373)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:171)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:161)
    at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:127)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:288)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:151)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4013)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2902)
    at com

Instead of defining your own deserializer, you could add jackson-datatype-joda to your dependencies and then register the JodaModule in your ObjectMapper :

ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JodaModule());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM