jackson-databind: Registration of `jackson-datatype-jsr310` not working in Jackson 2.12.0

Describe the bug On 2.12.0 (2.11.3 doesn’t have this bug), findAndRegisterModules does not work for com.fasterxml.jackson.datatype:jackson-datatype-jsr310.

Exception in thread "main" java.lang.RuntimeException: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Java 8 date/time type `java.time.Duration` not supported by default: add Module "com.fasterxml.jackson.datatype:jackson-datatype-jsr310" to enable handling
 at [Source: (String)"{ "duration": "PT5M" }"; line: 1, column: 15] (through reference chain: me.retrodaredevil.solarthing.program.SolarMain$TestObject["duration"])
        at me.retrodaredevil.solarthing.program.SolarMain.doMain(SolarMain.java:227)
        at me.retrodaredevil.solarthing.program.SolarMain.main(SolarMain.java:275)
Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Java 8 date/time type `java.time.Duration` not supported by default: add Module "com.fasterxml.jackson.datatype:jackson-datatype-jsr310" to enable handling
 at [Source: (String)"{ "duration": "PT5M" }"; line: 1, column: 15] (through reference chain: me.retrodaredevil.solarthing.program.SolarMain$TestObject["duration"])
        at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:67)
        at com.fasterxml.jackson.databind.DeserializationContext.reportBadDefinition(DeserializationContext.java:1766)
        at com.fasterxml.jackson.databind.deser.impl.UnsupportedTypeDeserializer.deserialize(UnsupportedTypeDeserializer.java:36)
        at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:542)
        at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeWithErrorWrapping(BeanDeserializer.java:565)
        at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeUsingPropertyBased(BeanDeserializer.java:449)
        at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1390)
        at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:362)
        at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:195)
        at com.fasterxml.jackson.databind.deser.DefaultDeserializationContext.readRootValue(DefaultDeserializationContext.java:322)
        at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4591)
        at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3546)
        at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3514)

Version information 2.12.0 on Java 11

To Reproduce

private static class TestObject {
	private TestObject(@JsonProperty("duration") Duration duration) {

	}
}

ObjectMapper mapper = new ObjectMapper();
mapper.findAndRegisterModules();
mapper.readValue("{ \"duration\": \"PT5M\" }", TestObject.class);

Expected behavior It should find the module and not throw the error above.

Additional context I tried putting my code in a JUnit5 test, and it passed perfectly, so this bug isn’t something that can be detected by unit tests. I’m using the shadow gradle plugin to compile my code into a fat jar, so something changed between 2.11 and 2.12 that causes this.

Also, adding the line mapper.registerModule(new JavaTimeModule()); fixes it, so I can guarantee that it exists on my classpath.

I wish I had some more info on why this is happening, but seeing how I can’t reproduce this in a unit test, I don’t really have any idea what the problem is.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 2
  • Comments: 18 (8 by maintainers)

Most upvoted comments

Adding dependency

<dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-jdk8</artifactId>
</dependency>

helps me to solve the issue.

For future reference for those using the shadow plugin to build their jars, just add mergeServiceFiles() to your shadowJar { } config. https://stackoverflow.com/questions/32887966/shadow-plugin-gradle-what-does-mergeservicefiles-do#32902274

That was an incredibly simple fix.

If I find the time, I’ll try creating a project that creates a jar file with this problem since I can’t reproduce it in a simple unit test, either.

Like I said, if I use these dependencies:

api "com.fasterxml.jackson.core:jackson-core:2.12.0"
api "com.fasterxml.jackson.core:jackson-annotations:2.12.0"
api "com.fasterxml.jackson.core:jackson-databind:2.12.0"
api "com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.11.3"

It does work. But if I use

api "com.fasterxml.jackson.core:jackson-core:2.12.0"
api "com.fasterxml.jackson.core:jackson-annotations:2.12.0"
api "com.fasterxml.jackson.core:jackson-databind:2.12.0"
api "com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.12.0" // change here

It does not work.

There’s also a possibility that this error doesn’t come up if you’re using Java 8 (I tested with Java 11).

It’s possible I’ll find the time sometime in the next day, or next few days. I don’t really have an idea right now.