杰克逊日期反序列化来自JSON文件的错误

时间:2019-05-25 14:17:36

标签: java json jackson

我正在尝试将JSON文件反序列化为POJO对象,但出现错误。

JSON文件看起来像(NotificationPOST.json)

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

POJO类具有

 @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;

自定义DateSerializer类

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

我尝试运行的测试类

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

我得到的错误

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

1 个答案:

答案 0 :(得分:2)

您可以将jackson-datatype-joda添加到依赖项中,然后在JodaModule中注册ObjectMapper,而不是定义自己的解串器:

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