question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:16 (8 by maintainers)

github_iconTop GitHub Comments

8reactions
bvn13commented, Mar 21, 2022

Adding dependency

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

helps me to solve the issue.

4reactions
retrodaredevilcommented, Dec 19, 2020

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

java - InvalidDefinitionException with jackson-datatype-jsr310 ...
This is resolved. Apparently, by adding the dependency, but not registering the JavaTimeModule, the OffsetDateTime gets serialized as a POJO ...
Read more >
com.fasterxml.jackson.datatype:jackson-datatype-jsr310
version published direct vulnerabilities 2.14.1 21 Nov, 2022 0. C. 0. H. 0. M. 0. L 2.14.0 5 Nov, 2022 0. C. 0. H. 0....
Read more >
Re: [jackson-dev] Re: Jackson 2.12.1 released
We would need a reproduction of the issue to help: after the module is registered, serialization and deserialization should work and there are ......
Read more >
JavaTimeModule (Jackson datatype: JSR310 2.8.11 API)
Class that registers capability of serializing java.time objects with the Jackson core. ObjectMapper mapper = new ObjectMapper(); mapper.
Read more >
`java.time.instant` not supported by default: add module "com ...
to the ObjectMapper, and then this started working. I'm not sure why I had to ... Jackson module not registered after update from...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found