Comprehensive Guide to Spring Boot, Spring MVC, and JPA Annotations
This article provides a detailed overview of common Spring Boot, Spring MVC, and JPA annotations—including their purposes, usage patterns, and code examples—helping developers understand how to configure, map, and manage beans, controllers, and persistence in Java backend applications.
Annotations List
A quick enumeration of frequently used Spring annotations such as @SpringBootApplication, @Configuration, @EnableAutoConfiguration, @ComponentScan, @Component, @RestController, @Autowired, @PathVariable, @JsonBackReference, and @RepositoryRestResource.
Annotations Detailed Explanation
@SpringBootApplication : Combines @Configuration, @EnableAutoConfiguration, and @ComponentScan to enable automatic configuration and component scanning.
package com.example.myproject;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication // same as @Configuration @EnableAutoConfiguration @ComponentScan
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}@ResponseBody : Directly writes the method return value to the HTTP response body, typically used for RESTful APIs.
@RequestMapping("/test")
@ResponseBody
public String test(){
return "ok";
}@Controller : Marks a class as a Spring MVC controller; methods are usually mapped with @RequestMapping.
@Controller
@RequestMapping("/demoInfo")
public class DemoController {
@Autowired
private DemoInfoService demoInfoService;
@RequestMapping("/hello")
public String hello(Map<String,Object> map){
System.out.println("DemoController.hello()");
map.put("hello","from DemoController.helloHtml");
// renders hello.html or hello.ftl
return "/hello";
}
}@RestController : Shortcut for @Controller + @ResponseBody, used for REST‑style controllers.
@RestController
@RequestMapping("/demoInfo2")
public class DemoController2 {
@RequestMapping("/test")
public String test(){
return "ok";
}
}@RequestMapping : Defines URL routing for controllers and methods, supporting attributes like params, headers, value, method, consumes, and produces.
@RequestParam and @PathVariable : Bind request parameters and path variables to method arguments.
@RequestMapping("user/get/mac/{macAddress}")
public String getByMacAddress(@PathVariable String macAddress){
// do something
}JPA Annotations
Key persistence annotations include @Entity, @Table, @MappedSuperclass, @Id, @GeneratedValue, @SequenceGenerator, @Column, @Transient, @JsonIgnore, @JoinColumn, and relationship mappings like @OneToOne, @OneToMany, @ManyToOne.
Spring MVC Related Annotations
Beyond @RequestMapping, Spring MVC uses @RequestParam for query/form parameters and @PathVariable for URL segments, enabling fine‑grained request handling.
Global Exception Handling
@ControllerAdvice marks a class for cross‑cutting concerns such as centralized exception handling, while @ExceptionHandler defines methods to process specific exception types.
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.
Java Architect Essentials
Committed to sharing quality articles and tutorials to help Java programmers progress from junior to mid-level to senior architect. We curate high-quality learning resources, interview questions, videos, and projects from across the internet to help you systematically improve your Java architecture skills. Follow and reply '1024' to get Java programming resources. Learn together, grow together.
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.
