Jackson deserialization issue for ZonedDateTime

I have the following field in a class that I use during deserialization of the service I consume.

private ZonedDateTime transactionDateTime;

The service I use can return a Date or DateTime using a template: yyyy-MM-dd'T'HH:mm:ss.SSSZ

Let me give you 2 examples of what the service returns:

  • 2015-11-18T18: 05: 38,000 + 0200
  • 2015-11-18T00: 00: 00,000 + 0200

While the former works well, the latter raises the following exception during deserialization:

java.time.format.DateTimeParseException: text '2015-11-18T00: 00: 00.000 + 0200' could not be parsed by index 23

I use;

  • Spring Download 1.3.1
  • Jackson 2.6.4 (with JSR310 enabled)

Does this require a custom deserialization class?

+4
2

@JsonFormat, , , JavaDocs.

, . , ( XML -), . :

com.fasterxml.jackson.databind.JsonMappingException: [ , com.foo.bar.adapter.john.model.account.UserAccount] String ('2015-11-18T00: 00: 00.000 + 0200'); /factory

:

@JsonFormat(pattern = Constants.DATETIME_FORMAT)
@JacksonXmlProperty(localName = "transactionDate")
private ZonedDateTime transactionDateTime;

@JsonRootName("transaction") , .

+1

, :

@JsonSerialize(using = MyCustomJsonDateSerializer.class)

@JsonDeserialize(using = MyCustomJsonDateDeserializer.class)

, . Serializer Deserializer JsonSerializer JsonDeserializer. :

public class MyCustomJsonDateSerializer extends JsonSerializer<Date> {

    @Override
    public void serialize(Date date, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
        jgen.writeString(date != null ? ISODateTimeFormat.dateTime().print(new DateTime(date)) : null);
      }
}
+2

Source: https://habr.com/ru/post/1624088/


All Articles