diff --git a/src/main/java/com/example/demo/ReflectionController.java b/src/main/java/com/example/demo/ReflectionController.java index 752a493..8bfcb5e 100644 --- a/src/main/java/com/example/demo/ReflectionController.java +++ b/src/main/java/com/example/demo/ReflectionController.java @@ -1,12 +1,17 @@ package com.example.demo; -import org.springframework.aot.hint.annotation.Reflective; +import org.springframework.aot.hint.RuntimeHints; +import org.springframework.aot.hint.RuntimeHintsRegistrar; +import org.springframework.context.annotation.ImportRuntimeHints; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import java.util.Arrays; import java.util.stream.Collectors; +import com.example.demo.ReflectionController.ReflectionControllerHints; + @RestController +@ImportRuntimeHints(ReflectionControllerHints.class) public class ReflectionController { @GetMapping("/reflection") @@ -14,7 +19,6 @@ public String message() { return getMessage(); } - @Reflective private String getMessage() { try { String className = Arrays.asList("com", "example", "demo", "Message").stream().collect(Collectors.joining(".")); @@ -23,4 +27,13 @@ private String getMessage() { return "Got an error: " + e.getMessage(); } } + + static class ReflectionControllerHints implements RuntimeHintsRegistrar { + + @Override + public void registerHints(RuntimeHints hints, ClassLoader classLoader) { + hints.reflection().registerType(Message.class, type -> type.withField("MESSAGE")); + } + } + } \ No newline at end of file