Search code examples
javaspringgradlebuild.gradlegradlew

Update existing Gradle Libs dependencies to marshal ZonedDateTime with Jackson 2.8.5 and DynamoDB in Spring Project


I've been tasked with converting all of our existing repositories from Joda Time to Java 8 time and I've hit quite a few snags on the way. The first involved DynamoDB not inherently supporting Java 8 so I made a custom converter for ZonedDateTime with some help from the web. Results here:

static public class ZonedDateTimeConverter implements DynamoDBTypeConverter<String, ZonedDateTime> {

    @Override
    public String convert(final ZonedDateTime time) {
        return time.toString();
    }

    @Override
    public ZonedDateTime unconvert(final String stringValue) {
        return ZonedDateTime.parse(stringValue);
    }
}

So that takes care of marshaling from ZonedDateTime to String and back for DynamoDB. But my last problem now is with marshaling from ZonedDateTime to String for Spring / Jackson dependency injection (I believe. Still pretty new to all this stuff).

Now according to Stackoverflow in order to do that I need jackson-datatype-jsr310 which is here. But at that page it says all I need to be able to marshal ZonedDateTime is Jackson 2.8.5

Now in libs.gradle I can see that we're using Jackson 2.5.0 jackson: 'com.fasterxml.jackson.core:jackson-databind:2.5.0', so that makes sense just need to update it right?

So I update the libs.gradle to now say jackson: 'com.fasterxml.jackson.core:jackson-databind:2.8.5', and I've added libs.jackson to the build.gradle file's compile section: compile(libs.jackson)

But I still receive com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of java.time.ZonedDateTime: no suitable constructor found, can not deserialize from Object value (missing default constructor or creator, or perhaps need to add/enable type information?) when building.

I've also tried adding jackson core and jackson databind to the ext.libs definition:

    jacksonCore: 'com.fasterxml.jackson.core:jackson-core:2.8.5',
    jacksonBind: 'com.fasterxml.jackson.core:jackson-databind:2.8.5',

and the build.gradle:

    libs.jacksonCore,
    libs.jacksonBind

Still no dice. Any ideas what's going on?


Solution

  • I figured it out! Here's my

    build.gradle:

    compile(
        libs.jacksonCore,
        libs.jacksonBind,
        libs.jacksonData
    )
    

    And libs.gradle:

        jacksonCore: 'com.fasterxml.jackson.core:jackson-core:2.8.8',
        jacksonBind: 'com.fasterxml.jackson.core:jackson-databind:2.8.8',
        jacksonData: 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.8.8',
    

    I thought jackson-datetype-jsr310 was pre-built into jackson-core or jackson-databind but apparently it isn't.