Я пытаюсь десериализовать 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