Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot 3 native image with Jackson

I'm trying to setup a new application with latest SpringBoot 3 and everything works fine until I try to create and run my application with Native compilation. Just for your reference here is error that I receive from running unit tests:

     Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `*****`: cannot deserialize from Object value (no delegate- or property-based Creator): this appears to be a native image, in which case you may need to configure reflection for the class that is to be deserialized
 at [Source: (String)"[{"T":"success","msg":"authenticated"}]"; line: 1, column: 3] (through reference chain: java.util.ArrayList[0])
       com.fasterxml.jackson.databind.DeserializationContext.reportBadDefinition(DeserializationContext.java:1909)
       com.fasterxml.jackson.databind.DatabindContext.reportBadDefinition(DatabindContext.java:408)
       com.fasterxml.jackson.databind.DeserializationContext.handleMissingInstantiator(DeserializationContext.java:1349)
       com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1417)
       com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:352)

I'm trying to convert JSON string to Object. It works fine when I run it as regular JAR , but it fails with I compile with Navite (GraalVM).

like image 943
Behrad Avatar asked May 23 '26 07:05

Behrad


2 Answers

in spring native, Jackson doesn't know how to serialize/deserialize objects without prior knowledge about them, since it must know all the types in compile time.
to run your app in a native mode, you will need to register hints with knowledge about needed proxies, reflected methods, additional resource files/paths, or objects to serialize/deserialize.
to do so, implement the RuntimeHintsRegistrar interface. you can see an example in the docs

like image 54
Benaya Trabelsi Avatar answered May 25 '26 07:05

Benaya Trabelsi


You may want to use @RegisterReflectionForBinding annotation for JSON mapping with jackson while working with RestTemplate or WebClient. More information in the docs

@RegisterReflectionForBinding({MyObject.class})
like image 41
Sagar Ahuja Avatar answered May 25 '26 06:05

Sagar Ahuja