Skip to main content

Practice ยท 2 of 3

Annotation-driven dispatch

Inside Solution: 1. Declare @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface Route { String value(); } 2. Add three STATIC methods annotated with routes: @Route("/home") static String serveHome(), @Route("/about") static String serveAbout(), @Route("/help") static String serveHelp() returning "home-page", "about-page", "help-page" respectively. (Method names deliberately differ from the route paths โ€” the mapping must come from the annotation.) 3. static Map<String, String> routeTable() โ€” reflect over getDeclaredMethods, map each @Route value to the METHOD NAME that carries it. 4. static String dispatch(String path, String body) โ€” look up the handler method name for path in routeTable(); if found, invoke it reflectively (it's static, no receiver) and return the result; if unknown, return exactly "404". The body arg is unused (routes take no input) โ€” keep it for signature stability.

Difficulty: advanced

Back to lesson: Practice: Reflection & DI drills