Quick revision
The short answer
The quickest way is @RegisterReflectionForBinding(MyDto.class) on a configuration class or bean, which registers the type and its members for the reflection Jackson uses. The more flexible way is a RuntimeHintsRegistrar that calls hints.reflection().registerType(MyDto.class, ...) with all or selected MemberCategory values, imported with @ImportRuntimeHints(MyHints.class). Either way, we verify the hints in a fast JVM unit test with RuntimeHintsPredicates. Types that appear in controller method signatures are often registered automatically, but check your version.
What to remember
In this answer, we saw that Jackson's reflection on a DTO fails in a native image unless the type's members are registered. The first way is @RegisterReflectionForBinding on a configuration class; the second is a RuntimeHintsRegistrar that calls hints.reflection().registerType with chosen MemberCategory values, imported with @ImportRuntimeHints. We tested the registrar with RuntimeHintsPredicates on the JVM and saw that types in controller signatures are usually registered automatically.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The quickest way is @RegisterReflectionForBinding(MyDto.class) on a configuration class or bean, which registers the type and its members for the reflection Jackson uses. The more flexible way is a RuntimeHintsRegistrar that calls hints.reflection().registerType(MyDto.class, ...) with all or selected MemberCategory values, imported with @ImportRuntimeHints(MyHints.class). Either way, we verify the hints in a fast JVM unit test with RuntimeHintsPredicates. Types that appear in controller method signatures are often registered automatically, but check your version.
Why this question is asked
Jackson builds objects from JSON by reflection: it finds a constructor, reads the fields or accessors and calls them. In a GraalVM native image, reflection only works on classes and members that were registered at build time, so a DTO that Jackson reaches only through a generic call can fail at run time. Interviewers ask for the two ways to register hints because they show whether we know Spring's hint API and when each form fits. In this answer, we will see the failure first, then write both forms of the fix, check them with a test, and see when Spring already does the work for us.
1A Quick Recap of Why Hints Are Needed
native-image includes only the code its static analysis can reach, and it only allows reflection on members that are listed in reachability metadata. Spring Boot's AOT step writes that metadata for everything it can see in the application context. But a DTO that is only ever passed to objectMapper.readValue(json, PriceUpdate.class) inside a method body may be reachable as a class without its constructor and accessors being registered for reflection. On the JVM this works; in the native image Jackson cannot see the members.
The exact exception text depends on the Jackson version; what matters is that the members needed for binding were not registered.
2Way 1: @RegisterReflectionForBinding
This annotation, from Spring Framework's aot.hint.annotation package, is placed on a @Configuration class or any bean class. During AOT processing Spring registers each listed type, and the types it refers to, for the kind of reflection that data binding libraries need: constructors, fields, getters and setters or record accessors. It is short and reads well next to the code that uses the DTOs.
Notice that the annotation has no effect on the JVM at all; it only feeds the AOT step. That makes it safe to add to any service.
3Way 2: A RuntimeHintsRegistrar
A RuntimeHintsRegistrar is a small class with one method, registerHints, that receives a RuntimeHints object and adds to it. It can register reflection, resources, proxies and serialization, can loop over a list of types, and can choose exactly which kinds of members to open. MemberCategory names those kinds: for example, invoke the declared constructors, invoke the public methods, access the declared fields. MemberCategory.values() opens everything, which is simple but makes the image a bit larger. Some field categories were renamed in Spring Framework 7, so check your version for the exact names. We import the registrar with @ImportRuntimeHints on a configuration class.
| Need | @RegisterReflectionForBinding | RuntimeHintsRegistrar |
|---|---|---|
| A few DTOs for JSON | best fit, one line | works, more code |
| Precise member categories | no, binding set only | yes |
| Resources, proxies, serialization | no | yes |
| Computed list of types | no | yes, with a loop |
| Shipped inside a library | possible | usual choice, via aot.factories |
The registrar is the better choice when the list of types comes from somewhere, such as all subclasses of an event base type, when we also need resource or proxy hints, or when a library wants to ship hints for its users.
4Verifying the Hints in a Unit Test
A native build takes minutes, so we do not want to find a missing hint there. RuntimeHintsPredicates builds predicates that check a RuntimeHints object, and AssertJ can assert them. The test runs on the JVM in milliseconds: we create an empty RuntimeHints, run our registrar on it and check that the types and members we need are there.
This test protects against someone removing a type from the registrar. It cannot prove that the list is complete, which is why the native tests (nativeTest) still run in CI.
5When Spring Registers Hints for Us
Spring infers many hints without help. For web controllers, types used as @RequestBody parameters and as return values of @ResponseBody or @RestController methods are registered for binding by the AOT processing of controller mappings. @ConfigurationProperties classes are registered too, and so are the beans themselves. So a DTO that only appears in a controller signature usually needs nothing; check your Spring version, because the inference has grown over releases. Hints are needed for types reached in less visible ways: inside method bodies, through messaging, through generic wrappers or by name.
6Follow-up questions
What does @Reflective do compared with @RegisterReflectionForBinding?
@Reflective marks the annotated element itself, such as a method or class, for reflection, and is often used as a meta-annotation by frameworks. @RegisterReflectionForBinding registers other types, the ones we list, for the binding style of reflection. For DTOs used by Jackson, the second is the usual choice.
Is MemberCategory.values() a bad idea?
It is not wrong, just broad. It opens every constructor, method and field of the type, which slightly increases image size and reflective surface. For a handful of DTOs it does not matter; for a library registering hundreds of types, choosing precise categories keeps the image smaller.
Where do the hints end up?
During the AOT step Spring collects all hints and writes them as GraalVM reachability metadata in the generated resources, under META-INF/native-image for the application's group and artifact. native-image then reads those files during the build.
7Conclusion
In this answer, we saw that Jackson's reflection on a DTO fails in a native image unless the type's members are registered. The first way is @RegisterReflectionForBinding on a configuration class; the second is a RuntimeHintsRegistrar that calls hints.reflection().registerType with chosen MemberCategory values, imported with @ImportRuntimeHints. We tested the registrar with RuntimeHintsPredicates on the JVM and saw that types in controller signatures are usually registered automatically.
Open with
The first way is the annotation @RegisterReflectionForBinding. I put it on a configuration class or a bean and list the DTO classes, for example @RegisterReflectionForBinding(PriceUpdate.class). During AOT processing Spring registers those types, and what they reference, for the reflection that data binding needs, so constructors, fields and accessors.
Then, if they want more
It's one line and it has no effect on the JVM. The second way is a RuntimeHintsRegistrar. I implement registerHints, and call hints.reflection().registerType(PriceUpdate.class, ...) with either MemberCategory.values() or specific categories like INVOKE_DECLARED_CONSTRUCTORS and INVOKE_PUBLIC_METHODS for a record. Then I attach it with @ImportRuntimeHints(MyHints.class) on a configuration class.
The registrar is more flexible: it can loop over a computed list of types, register resources and proxies as well, and libraries can ship it. To check it, I write a plain JVM unit test that creates an empty RuntimeHints, runs my registrar and asserts RuntimeHintsPredicates.reflection().onType(PriceUpdate.class) accepts the hints.
It runs in milliseconds, so I find a missing registration long before the slow native build. And I'd mention that types in @RestController signatures are usually registered automatically by Spring, so hints are mainly needed for DTOs used inside method bodies or through messaging.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Spring Framework 7
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Kubernetes documentation, Spring Boot reference
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Show two ways to register reflection hints for a class that Jackson deserializes in a...": https://scrivio-interview-library.netlify.app/spring-boot/native-and-aot/dbeeac58bc/