Lab 12: Annotation-Driven Frameworks

Objective

Build a mini annotation-driven framework: custom @RestController/@GetMapping/@PostMapping/@PathVar annotations, runtime annotation scanning, automatic route registration from class structure, request dispatching via reflection, and a validation framework using @Validate method annotations.

Background

Every Java web framework (Spring MVC, Jakarta EE, Quarkus, Micronaut) discovers routes and beans through annotation scanning at startup. By building a simplified version, you understand exactly what happens when Spring sees @GetMapping("/products") — it uses reflection to find annotated methods, builds a routing table, and dispatches requests via Method.invoke(). Micronaut and Quarkus do this at build-time via annotation processors (APT).

Time

30 minutes

Prerequisites

  • Practitioner Lab 12 (Reflection & Annotations)

Tools

  • Docker: zchencow/innozverse-java:latest


Lab Instructions

Steps 1–8: Define annotations, annotate controller, build route registry, dispatch, validation framework, middleware injection, JSON response, Capstone

💡 This is exactly what Spring Boot does at startup. @SpringBootApplication triggers classpath scanning, which finds @RestController classes, inspects their methods for @GetMapping/@PostMapping, extracts @PathVariable parameters, and builds a routing table. The only difference: Spring uses a much more sophisticated path-variable parser, content negotiation, and injects additional context like HttpServletRequest. The reflection core is identical.

📸 Verified Output:


Summary

Pattern
How Spring/Quarkus does it

Route discovery

Scan classpath for @RestController

Method dispatch

Method.invoke(controller, args)

Path variable

@PathVariable → param annotation

Validation

@Valid → constraint annotations

Bean injection

@Autowired → field/constructor injection

Further Reading

Last updated