-->

Jackson json formatting of zoneddatetime

2019-07-29 12:20发布

问题:

I need to define the JsonFormat of a ZonedDateTime in which the time is defined in a xml message.

I added the Jackson jar to format ZonedDateTime (com.fasterxml.jackson.datatype.jsr310.JavaTimeModule)

Added the JavaTimeModule like this:

XmlMapper xmlMapper = new XmlMapper();
xmlMapper.registerModule(new JavaTimeModule());
xmlMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);

The problem is that it cannot format the datetime correctly. The error is:

com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type java.time.ZonedDateTime from String "2019 3 4 14 44 20 -5": Text '2019 3 4 14 44 20 -5' could not be parsed at index 18

This is how I am defining the time pattern:

@JsonFormat(pattern = "yyyy M e k m s X")
private ZonedDateTime openDtGmt;

How can I map the zone pattern when it has a negative in the string?

UPDATE I realize that the date is not consistent with conventions. But this is coming from a legacy system and cannot be changed. We need to identify the date/time pattern to be this: 4 digit year; all remaining digits do NOT have leading zeros; then a timezone without leading zeros and a sign before the digit. Also, the timezone is GMT time. The format for the timezone is UTC.

We replaced XmlMapper with ObjectMapper. That seems to get us past the date/time format error but within the XML there is an empty tag in which it is failing. If we can get past the empty tag error, we can parse the date/time with our own method.

UPDATE After more testing, it seems that the error we are getting when using the ObjectMapper has to do with an empty tag. It seems that the xml string is too long. It errors at the 1000 character length.