10

I've the following field in a class I use during deserialization of a service that I'm consuming.

private ZonedDateTime transactionDateTime;

The service I'm consuming may return a Date or DateTime using the pattern: yyyy-MM-dd'T'HH:mm:ss.SSSZ

Let me give 2 examples of what the service returns:

  • 2015-11-18T18:05:38.000+0200
  • 2015-11-18T00:00:00.000+0200

While first one works well, the latter causes the following exception to be thrown during deserialization:

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

I'm using;

  • Spring Boot 1.3.1
  • Jackson 2.6.4 (with JSR310 module included)

Does this require a custom deserialization class?

Alper Kanat
  • 376
  • 1
  • 2
  • 16

5 Answers5

7

Jackson deserializer will by default bypass the timezone information and use context timezone to override it, which all ISO8601 will ends to UTC.

Using Spring, this feature can be turned off by:

spring.jackson.deserialization.ADJUST_DATES_TO_CONTEXT_TIME_ZONE=false
Eyal Schneider
  • 22,166
  • 5
  • 47
  • 78
Jiajun Cui
  • 71
  • 1
  • 1
  • or if using jackson mapper `new ObjectMapper().registerModule(new JavaTimeModule()).disable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE)` – prayagupa Feb 21 '20 at 01:16
5

You can use annotations like:

@JsonSerialize(using = MyCustomJsonDateSerializer.class)

or

@JsonDeserialize(using = MyCustomJsonDateDeserializer.class)

To customize how Jackson parses Dates. Those custom Serializer and Deserializer must extend JsonSerializer and JsonDeserializer. For example:

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);
      }
}
Ricardo Vila
  • 1,626
  • 1
  • 18
  • 34
4

Earlier in the code I was using the field with @JsonFormat annotation but removed that as I thought it was meant for serialization only like the JavaDocs suggest.

Turned out that I needed add back that annotation. And the real issue was that the 3rd party service response was indeed wrong (it was missing a wrapper element in the XML) which caused the deserialisation to fail. The error was:

com.fasterxml.jackson.databind.JsonMappingException: Can not instantiate value of type [simple type, class com.foo.bar.adapter.john.model.account.UserAccount] from String value ('2015-11-18T00:00:00.000+0200'); no single-String constructor/factory method

The field is written like below:

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

Also I had to add @JsonRootName("transaction") to the class of this field because the object is wrapped into a collection.

Alper Kanat
  • 376
  • 1
  • 2
  • 16
1

I've used

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ssXXX")
private ZonedDateTime startDate;

plus jackson-datatype-jsr310 library, obviously.

This solution is described in Jackson deserialize ISO8601 formatted date-time into Java8 Instant

Vova Rozhkov
  • 1,582
  • 2
  • 19
  • 27
1

The following configuration helped me

Specify datetime pattern:

public class Timestamp {

    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ssXXX")
    private ZonedDateTime timestamp;

}

Disable converting ZonedDateTime to UTC:

objectMapper.configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE, false);

Also there was another problem, which may be tricky to spot. In my case I was receiving Timestamp from another micro service using RestTemplate. RestTemplate can be configured to use notProjectDefaultObjectMapper, which is not affected by default Spring Jackson configuration approaches (like application properties, or @Configuration classes with Jackson2ObjectMapperBuilderCustomizer bean definition or somehow else). So RestTemplate's objectMapper (if there is one) should be configured too