Comprehensive Guide to Spring MVC Annotations and Related Spring Annotations
This article provides a detailed overview of Spring MVC and Spring Boot annotations such as @RequestMapping, @GetMapping, @PostMapping, @RequestBody, @ControllerAdvice, @Component, @Bean, @Scope, @Autowired, and others, explaining their purposes, attributes, usage patterns, and offering practical code examples for each.
The article introduces the purpose of the @RequestMapping annotation, which maps web requests to handler methods, and lists its six attributes: value, method, params, header, consume, and produce, with brief explanations for each.
It then shows a complete example class annotated with @RestController that demonstrates various @RequestMapping configurations, including GET, POST, parameter filtering, and response content types.
Subsequent sections cover other request‑mapping shortcuts: @GetMapping, @PostMapping, @PutMapping, @DeleteMapping, and @PatchMapping, each described as a convenient alias for @RequestMapping with a specific HTTP method.
The article explains @RequestBody for binding request bodies to objects, the optional use of @Valid for validation, and provides a @PostMapping example that creates a user.
It details @ControllerAdvice and its companion annotations @ExceptionHandler, @InitBinder, and @ModelAttribute, illustrating how to define a global exception handler and custom data binding logic.
Further, the guide covers component‑scanning annotations: @Component, @Service, @Repository, and @ComponentScan, explaining their role in bean registration.
The dependency‑injection section discusses @Autowired, @Primary, @Qualifier, and lifecycle annotations @PostConstruct and @PreDestroy, with sample code showing initialization and cleanup methods.
Bean definition is covered with @Bean, including initMethod and destroyMethod attributes, and the use of @Scope to control bean lifecycles (singleton, prototype, etc.).
Finally, the article introduces Spring Boot’s core annotations: @SpringBootApplication, @EnableAutoConfiguration, and various conditional annotations ( @ConditionalOnClass, @ConditionalOnMissingClass, @ConditionalOnBean, @ConditionalOnMissingBean, @ConditionalOnProperty, @ConditionalOnResource, @ConditionalOnWebApplication, @ConditionalOnNotWebApplication, @ConditionalExpression, and @Conditional), describing how they enable fine‑grained auto‑configuration based on classpath, bean presence, properties, resources, and custom conditions.
The article concludes with a summary stating that it has covered the most common Spring Boot annotations, providing a unified reference for developers.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Architect
Professional architect sharing high‑quality architecture insights. Topics include high‑availability, high‑performance, high‑stability architectures, big data, machine learning, Java, system and distributed architecture, AI, and practical large‑scale architecture case studies. Open to ideas‑driven architects who enjoy sharing and learning.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
