how to resolve Joda date/time type `org.joda.time.DateTime` not supported by default
Asked Answered
S

2

13

I have an existing web-app and unfortunately the 'time' fields in the DB are not converted to zulu time. Instead we are using org.joda.time.DateTime as our datetime and timezone encapsulator.

I am in the process of updating from hibernate 4.3.7.Final to 5.3.20.Final
Also, we are letting hibernate manage the translation of these DateTime objects to/from the db using

@Type(type="org.jadira.usertype.dateandtime.joda.PersistentDateTime")
private DateTime lastUpdatedTimestamp; // <-- org.joda.time.DateTime

Running this on hibernate 5 gives this error:

10:22:11,463 ERROR [stderr] (default task-2) com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Joda date/time type `org.joda.time.DateTime` not supported by default: add Module "com.fasterxml.jackson.datatype:jackson-datatype-joda" to enable handling (through reference chain: org.<company>.crs.model.PermissionEntity["lastUpdatedTimestamp"])

wondering why this would work in hibernate 4 before the update to 5? I am using joda-time version 2.1 but have been able to reproduce the issue in 2.9.7

I have also looked and found this thread Persist Joda-time's DateTime via Hibernate but, their solution was to convert to Joda's LocalDateTime which unless I am mistaken does not support time zones which are needed for my use case.

Edit: I have added the requested module, and I still get the same error. I have tested this with Joda versions: 2.10.10, 2.9.7, and 2.1

<dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-joda</artifactId>
    <version>2.12.3</version>
</dependency>
Sternberg answered 15/4, 2021 at 14:59 Comment(7)
I believe 'Joda date/time type org.joda.time.DateTime not supported by default: add Module "com.fasterxml.jackson.datatype:jackson-datatype-joda" to enable handling' is prettly clear. What help did you expect from us?Nauru
I am confused as to why it worked with nothing special in hibernate 4 but not 5?Sternberg
The error has nothing to do with Hibernate, it's thrown by JacksonNauru
This is probably just me being stupid, but, I added the Jackson module as an mvn dependency and I still get the same error.Sternberg
There's an XxxModule class provided by that dependency. You need to register the module by calling objectMapper.registerModule(new XxxModule())Nauru
Resolution: Thank you crizzis! I needed to only have 1 version of jackson-datatype-joda in my dependencies.Sternberg
I am facing the same issue. Have you been able to solve it?Valera
T
34

You have to register the JodaModule on your ObjectMapper, like this:

 import com.fasterxml.jackson.datatype.joda.JodaModule;

 ObjectMapper mapper = new ObjectMapper()
    .registerModule(new JodaModule())
    ...;

Prior to Jackson 2.12, you wouldn't get the error you're seeing because Jackson would default to using the BeanSerializer if it encountered a Joda object. Here's the code added in 2.12 that now raises an error instead:

https://github.com/FasterXML/jackson-databind/blob/2.13/src/main/java/com/fasterxml/jackson/databind/util/BeanUtil.java#L288

Termitarium answered 19/8, 2021 at 18:46 Comment(3)
In my case of older app it was the best to call registerModule() using Spring settings in XML file (with MethodInvokingFactoryBean).Hennebery
I still get the error even after registering the Joda module. objectMapper.registerModule(new JodaModule()) I also confirmed that there is only one ObjectMapper reference in the entire code base.Triny
how to solve this in spring MVC . object mapper is not something which we create in our code in this caseMonteverdi
L
0

This worked for me with Spring6. Update after default converters are built.

@Configuration
@EnableWebMvc
public class ServletWebConfig implements WebMvcConfigurer, ApplicationContextAware {

    ...

    /**
     * default converters = {ArrayList@34386}  size = 9
     *  0 = {ByteArrayHttpMessageConverter@34472}
     *  1 = {StringHttpMessageConverter@34473}
     *  2 = {ResourceHttpMessageConverter@34474}
     *  3 = {ResourceRegionHttpMessageConverter@34475}
     *  4 = {AllEncompassingFormHttpMessageConverter@34476}
     *  5 = {Jaxb2RootElementHttpMessageConverter@34477}
     *  6 = {MappingJackson2HttpMessageConverter@34460}
     *  7 = {MappingJackson2SmileHttpMessageConverter@34478}
     *  8 = {MappingJackson2CborHttpMessageConverter@34479}
     *
     * @param converters initially an empty list of converters
     */
    @Override
    public void configureMessageConverters(final List<HttpMessageConverter<?>> converters) {
        WebMvcConfigurer.super.configureMessageConverters(converters);
    }


    @Override
    public void extendMessageConverters(final List<HttpMessageConverter<?>> converters) {
        for (final HttpMessageConverter<?> converter : converters) {
            if (converter instanceof MappingJackson2HttpMessageConverter) {
                final ObjectMapper objectMapper = ((MappingJackson2HttpMessageConverter)converter).getObjectMapper();
                objectMapper.registerModule(new JodaModule()); // <--- inject Joda de/ serializers
            }
        }

        WebMvcConfigurer.super.extendMessageConverters(converters);
    }

    ...
}
Lafollette answered 19/7 at 18:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.