3

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).

Benaya Trabelsi
  • 351
  • 3
  • 11
Behrad
  • 33
  • 1
  • 4

2 Answers2

5

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

Benaya Trabelsi
  • 351
  • 3
  • 11
  • 1
    thank you for the help and documentation, I was missing this part of the documentation about how to provide the HINT to native image !!! – Behrad Dec 28 '22 at 02:00
1

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})
Sagar Ahuja
  • 637
  • 10
  • 10