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.

`InvalidDefinitionException: Java 8 date/time type java.time.LocalDateTime...` when calling `mapper.createObjectNode().putPOJO`

See original GitHub issue

Hello,

When running the code below I get the exception:

Exception in thread "main" java.lang.RuntimeException: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Java 8 date/time type `java.time.LocalDateTime` not supported by default: add Module "com.fasterxml.jackson.datatype:jackson-datatype-jsr310" to enable handling
	at com.fasterxml.jackson.databind.node.InternalNodeMapper.nodeToPrettyString(InternalNodeMapper.java:40)
	at com.fasterxml.jackson.databind.node.BaseJsonNode.toPrettyString(BaseJsonNode.java:141)
	at Main.main(Main.java:16)
Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Java 8 date/time type `java.time.LocalDateTime` not supported by default: add Module "com.fasterxml.jackson.datatype:jackson-datatype-jsr310" to enable handling
Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Java 8 date/time type `java.time.LocalDateTime` not supported by default: add Module "com.fasterxml.jackson.datatype:jackson-datatype-jsr310" to enable handling

	at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:77)
	at com.fasterxml.jackson.databind.SerializerProvider.reportBadDefinition(SerializerProvider.java:1300)
	at com.fasterxml.jackson.databind.ser.impl.UnsupportedTypeSerializer.serialize(UnsupportedTypeSerializer.java:35)
	at com.fasterxml.jackson.databind.SerializerProvider.defaultSerializeValue(SerializerProvider.java:1142)
	at com.fasterxml.jackson.databind.node.POJONode.serialize(POJONode.java:115)
	at com.fasterxml.jackson.databind.node.ObjectNode.serialize(ObjectNode.java:328)
	at com.fasterxml.jackson.databind.ser.std.SerializableSerializer.serialize(SerializableSerializer.java:39)
	at com.fasterxml.jackson.databind.ser.std.SerializableSerializer.serialize(SerializableSerializer.java:20)
	at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider._serialize(DefaultSerializerProvider.java:480)
	at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:319)
	at com.fasterxml.jackson.databind.ObjectWriter$Prefetch.serialize(ObjectWriter.java:1518)
	at com.fasterxml.jackson.databind.ObjectWriter._writeValueAndClose(ObjectWriter.java:1219)
	at com.fasterxml.jackson.databind.ObjectWriter.writeValueAsString(ObjectWriter.java:1086)
	at com.fasterxml.jackson.databind.node.InternalNodeMapper.nodeToPrettyString(InternalNodeMapper.java:38)
	... 2 more

2.11.4 works as expected. 2.13.0-rc2 fails with the same error.

Main.java

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;

import java.time.LocalDateTime;

public class Main {

    public static void main(String[] args) {
        ObjectMapper mapper = JsonMapper.builder()
            .addModule(new JavaTimeModule())
            .build();

        ObjectNode node = mapper.createObjectNode().putPOJO("test", LocalDateTime.now());
        System.out.println(node.toPrettyString());
    }

}

build.gradle

plugins {
    id 'java'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    implementation platform("com.fasterxml.jackson:jackson-bom:2.12.5")
    implementation "com.fasterxml.jackson.core:jackson-databind"
    implementation "com.fasterxml.jackson.datatype:jackson-datatype-jsr310"
}

I’ve tried using AdoptOpenJDK (OpenJ9) 11.0.11 and AdoptOpenJDK (Hotspot) 14.0.2 and I get the same result on both.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
jebbenchcommented, Aug 31, 2021

I think I am adding the module:

ObjectMapper mapper = JsonMapper.builder()
            .addModule(new JavaTimeModule())
            .build();

It doesn’t actually work in 2.11, it just serializes the date without using the module.

I looks like calling any accessor methods on ObjectNode ignores any modules loaded in the Object Mapper which used to create it.

My use case is in a unit test checking that the ObjectNode is built correctly - in running code it’s not an issue as I always do objectMapper.writeValue but in my tests I just want to check the updatedAt key is correct (node.get("updatedAt")).

0reactions
cowtowncodercommented, Aug 30, 2022

At this point I can reproduce the issue but don’t really know if anything may be done – my suggestion is that when using “POJO” nodes, use mapper.writeValueAsString() and not rely on JsonNode.toString() as that can never really have support for external types.

The problem then being that of error handling as there seem to be only bad choices:

  1. Throw an unchecked exception (which is surprising from toString())
  2. Produce invalid JSON (like truncate, or add exception info)
  3. Produce valid JSON but one that is different from what one would get from proper serialization (like JSON String with error message)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Java 8 date/time type `java.time.Instant` not supported by default
I saw an issue in one of my test classes. The problem there was it was creating a new ObjectMapper instance that was...
Read more >
`java.time.localdatetime` not supported by default: add module ...
FasterXML/jackson-databind`InvalidDefinitionException: Java 8 date/time type java.time.LocalDateTime...` when calling `mapper.createObjectNode().putPOJO`# ...
Read more >
Jackson Error: Java 8 date/time type not supported by default
The error occurs when we serialize a Java object or deserialize JSON to POJO, and the POJO contains new Java 8 date time...
Read more >
Jackson Tips: ObjectNode.putPOJO(), putRawValue() for fun ...
Collection s and arrays to “Plain Old Java Object” (POJOs, aka Beans), as well as most scalar JDK types (Strings, Numbers, Booleans, Date/Time...
Read more >
Getting "Java 8 date/time type `java.time.OffsetDateTime` not ...
Make it easier to configure the used object mapper (the above is a bit of a hack because it modifies the global mapper...
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