Search code examples
javajsonspringspring-bootspring-camel

Spring boot camel unmarshal json list of objects that have ZonedDateTime field


I have 2 different camel routes.

1)

from(...).routeId(...)
  .unmarshal().json(SomeClass.class)
  ....;

And 2)

from(...).routeId(...)
  .unmarshal(new ListJacksonDataFormat(SomeOtherClass.class))
  ....;

Both SomeClass and SomeOtherClass have field named timestamp whose type is java.time.ZonedDateTime.

In first route I pass single object and it unmarshals without errors. In 2nd route I pass a list of objects and it does not work:

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Java 8 date/time type java.time.ZonedDateTime not supported by default: add Module "com.fasterxml.jackson.datatype:jackson-datatype-jsr310" to enable handling

Questions:

  1. Do I correctly understand that if I want to unmarshal list I need to use .unmarshal(new ListJacksonDataFormat(...)) instead of .unmarshal().json(...)?
  2. Do I really need to add Module com.fasterxml.jackson.datatype:jackson-datatype-jsr310? Otherwise I would have added it without even asking here but since my first route can manage ZonedDateTime just fine I wanted to be sure that I really have to add a new module or if there are any other means of dealing with it? It's strange that first route can unmarshal ZonedDateTime fine and 2nd can't.

After .unmarshal(...) I plan to call .bean(..., "method") where method takes List as argument. Not sure if this extra info is needed but added it just in case.


Solution

  • The likely issue is that in the first example the actual json which you are trying to unmarshal does not include a timestamp so jackson will not try to unmarshal a field of type java.time.ZonedDateTime.

    If it is your intention that the object be unmarshalled with the timestamp field populated you should add com.fasterxml.jackson.datatype:jackson-datatype-jsr310 to your project.