1

I hope someone can point out what I'm missing here.

I have a Spring Boot 2 App with a rest controller that takes a handful of request parameters, one of them being a java.time.Instant as "@RequestParam("offset") Instant offset,". I expected this to be deserializable from a second or millisecond based timestamp, like 1604303073000.

Yet I always get the mentioned exception, originating at org.springframework.format.datetime.standard.InstantFormatter.parse(InstantFormatter.java:50) ~[spring-context-5.2.2.RELEASE.jar:5.2.2.RELEASE]

The serialization of Instants to second or millisecond based timestamps works just fine with these parameters:

jackson:
  serialization:
    write-dates-as-timestamps: true
    write-date-timestamps-as-nanoseconds: true
  deserialization:
    read-date-timestamps-as-nanoseconds: false

But the opposite way seems to have no effect at all on the deserialization, no matter if it's set to true or false. Spring obviously just tries to apply my timestamps to the wrong Formatter.

All similar issues I could find seemed to imply that this is a no-brainer and the default behavior, yet this already caused some quite frustrating hours of trial and error.

godsim
  • 181
  • 1
  • 9
  • The problem is, that jackson has no default serializer and deserializer for `java.time`, you have to add those manually, by including the dependency (mentioned in the linked question), and registering the `JavaTimeModule()` to the objectmapper – Lino Nov 02 '20 at 08:24
  • Im quite sure they have at least the serializers as I use them for the Instants with the shown yaml config. I'll check out your suggestion, thx! – godsim Nov 02 '20 at 08:26
  • somwehow it will not use the jackson deserializer, no matter what I do. I have the dependency, created a custom Jackson module with serializer and deserialier and configured MappingJackson2HttpMessageConverter. Yet the default InstnatFormatter is still used. – godsim Nov 02 '20 at 09:11
  • have you added the `JavaTimeModule` I mentioned? Also be careful when you instantiate the `ObjectMapper` yourself. It may not be the one used by spring. Make sure that you register the module and your de-/serializers to the `ObjectMapper` -bean used by spring – Lino Nov 02 '20 at 09:13
  • I added jackson-modules-java8:2.11.3 and have @Primary @Bean(name = "jsonMapper") public ObjectMapper jsonMapper() { return new ObjectMapper().registerModule(new JavaTimeModule()); } – godsim Nov 02 '20 at 09:26
  • I got it solved finally, please adapt the reference for duplicate as it is not related to your presented answer. Should be this: https://stackoverflow.com/questions/29196466/convert-unix-timestamp-to-java-date-spring-requestparam since Jackson seemingly doenst care for request params. – godsim Nov 02 '20 at 10:46
  • Have you also looked at this question: https://stackoverflow.com/questions/40274353/how-to-use-localdatetime-requestparam-in-spring-i-get-failed-to-convert-string ? Seems to also match your case – Lino Nov 02 '20 at 12:13
  • Yeah, I found that one too, but it described a solution for formatted date(times) as query params, but not for epoch timestamps. At least I havent seen a formatter that would handle epochs and convert them to Instants. – godsim Nov 02 '20 at 13:03
  • 1
    anyways, thank you for your support! =) – godsim Nov 02 '20 at 13:03

0 Answers0