Comprehensive Guide to Spring Boot, Spring MVC, and JPA Annotations
This article provides a detailed overview of the most commonly used Spring Boot, Spring MVC, and JPA annotations, explains their purposes, shows how they interact, and includes practical code examples for building robust Java backend applications.
This article presents a systematic reference of Spring annotations used in backend development, covering Spring Boot meta‑annotations, component scanning, configuration, REST handling, dependency injection, and JPA mapping.
1. Spring Boot meta‑annotations
@SpringBootApplicationcombines @ComponentScan, @Configuration and @EnableAutoConfiguration. It tells Spring Boot to scan for @Configuration classes and register them in the application context. @ComponentScan enables automatic discovery of beans such as @Component, @Service, @Repository, etc. @Configuration is the Java‑based equivalent of an XML configuration file, providing type‑safe configuration. @EnableAutoConfiguration activates Spring Boot’s auto‑configuration mechanism based on the classpath and existing beans. @RestController is a shortcut for @Controller + @ResponseBody, indicating a REST‑style controller whose methods return data directly to the HTTP response body. @Autowired performs automatic dependency injection of beans. @PathVariable binds a URI template variable to a method parameter. @JsonBackReference solves circular reference problems during JSON serialization. @RepositoryRestResource works with spring-boot-starter-data-rest to expose repository methods as REST endpoints.
2. Detailed annotation explanations
@SpringBootApplicationis equivalent to applying @Configuration, @EnableAutoConfiguration and @ComponentScan together.
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);
}
} @ResponseBodyindicates that the method’s return value should be written directly to the HTTP response body, typically used for JSON or other RESTful responses.
@RequestMapping("/test")
@ResponseBody
public String test() {
return "ok";
} @Controllermarks a class as a Spring MVC controller that forwards requests to view templates.
@Controller
@RequestMapping("/demoInfo")
public class DemoController {
@Autowired
private DemoInfoService demoInfoService;
@RequestMapping("/hello")
public String hello(Map map) {
System.out.println("DemoController.hello()");
map.put("hello", "from TemplateController.helloHtml");
return "/hello"; // renders hello.html or hello.ftl
}
} @RestControllercombines @Controller and @ResponseBody for RESTful endpoints.
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/demoInfo2")
public class DemoController2 {
@RequestMapping("/test")
public String test() {
return "ok";
}
} @RequestMappingdefines URL routing for controllers and methods, supporting attributes such as params, headers, method, consumes, and produces. @RequestParam binds request parameters to method arguments, while @PathVariable extracts values from the URL path.
@RequestMapping("user/get/{macAddress}")
public String getByMacAddress(@PathVariable String macAddress) {
// do something
}3. JPA annotations
@Entitymarks a class as a JPA entity; optionally paired with @Table(name="") to specify the table name. @MappedSuperclass designates a superclass whose fields are inherited by entity subclasses. @NoRepositoryBean prevents Spring Data from instantiating a repository interface. @Column maps a field to a database column; it can be omitted when the names match. @Id denotes the primary key, and @GeneratedValue defines the generation strategy (e.g., SEQUENCE, AUTO, IDENTITY). @SequenceGenerator configures a database sequence for primary‑key generation. @Transient tells the ORM to ignore a field, preventing it from being persisted. @Basic(fetch=FetchType.LAZY) controls lazy loading of entity attributes. @JsonIgnore excludes a property from JSON serialization/deserialization. @JoinColumn, @OneToOne, @OneToMany, @ManyToOne define relationships between entities.
4. Global exception handling
@ControllerAdvicemarks a class as a global exception handler (it is also a @Component). @ExceptionHandler(Exception.class) on a method specifies the exception type that the method will handle.
5. Summary
By mastering these annotations, developers can efficiently configure Spring Boot applications, build RESTful APIs with Spring MVC, and map domain models to relational databases using JPA, resulting in clean, maintainable backend code.
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.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn 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.
