Java MongoDb getting id as a timestamp but need the hexadecimal string

The problem you face is ObjectId becomes extended (Deserialized). we make it serialization.Add this dependency in pom

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.11.0.rc1</version>
</dependency>

Add the following method in main class.

@Bean
public Jackson2ObjectMapperBuilderCustomizer customizer()
{
    return builder -> builder.serializerByType(ObjectId.class,new ToStringSerializer());
}

This will give you expected output.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top