0

I've spent the last weeks trying to get a project of mine work with Spring Native. One of my biggest problems is with serializing / deserializing objects using Jackson. I'm aware of the reason why this fails and that I can add native hints for any classes not detected in the analysis. Unfortunately this is a) a lot of work and b) error prone as I only detect the problem after having built the native image and then testing the code.

Spring Native already creates a lot of hints when the process is run during the AOT processing step. The documentation says "Static analysis of your application is performed at build-time from the main entry point.". This issue says "spring-native generates hints for classes returned by controller methods and follows the chain (via static analysis) to find referenced classes." So how exactly does that work? How can I know which classes are detected and for which I have to add hints while writing code? So far it seems to be trial and error for me. Is there a better way?

Edit: I created a ReflectionMarker annotation which I add to all classes I expect to be serialized / deserialized (basically all with the @Data annotation from Lombok). Then in my RuntimeHintsRegistrar I scan for those and add hints for public constructors and all declared methods. It works but still involves a manual step I can forget and feels hacky.

Pragmatick
  • 130
  • 16

1 Answers1

0

I would recommend to use and add all model classes you might need for JSON reflective use into @RegisterReflectionForBinding annotation’s list of classes. It’s bit of manual work but ensures it those classes are available for reflection during runtime.

Edit: Apparently you can also use @Reflective annotation but the default processor does not seem to work for Jackson JSON models too well. Instead you can use the same processor that @RegisterReflectionForBinding uses --> @Reflective(RegisterReflectionForBindingProcessor.class) to achieve it you can add this annotation for each model class separately and there is no need to maintain separate list of them.

When it comes to how well AOT build phase notices all these classes and acts accordingly is somewhat random if I may say so. It does a great job but there is not much exact knowledge what it will find and what not and mark the classes automatically for proper processing that works during application runtime too. And it may very well be safest to add hints for all the classes you know you need explicitly instead.

harism
  • 6,011
  • 1
  • 36
  • 31