Spring Boot Annotations: @SpringBootApplication, @RestController & JPA Basics
This article provides a comprehensive overview of essential Spring Boot and Spring MVC annotations—including @SpringBootApplication, @RestController, @RequestMapping, @Autowired, and JPA annotations—explaining their purposes, usage examples, and how they simplify configuration, component scanning, request handling, and database mapping in Java backend development.
1. Annotation List
@SpringBootApplication : combines @ComponentScan, @Configuration and @EnableAutoConfiguration.
@Configuration : equivalent to XML configuration, provides type‑safe Java config.
@EnableAutoConfiguration : enables Spring Boot’s auto‑configuration based on classpath.
@ComponentScan : scans for components such as @Component, @Service, @Repository.
@Component : generic stereotype for a Spring‑managed component.
@RestController : combination of @Controller and @ResponseBody for REST‑style controllers.
@Autowired : injects bean dependencies automatically.
@PathVariable : binds URI template variables to method parameters.
@JsonBackReference : solves circular reference problems during JSON serialization.
@RepositoryRestResource : works with spring‑boot‑starter‑data‑rest.
2. Detailed Annotation Usage
@SpringBootApplication : declares that Spring Boot should auto‑configure the application; equivalent to using @Configuration, @EnableAutoConfiguration and @ComponentScan together.
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 : writes the method return value directly to the HTTP response body, useful for returning JSON from asynchronous calls.
@RequestMapping("/test")
@ResponseBody
public String test() {
return "ok";
}@Controller and @RequestMapping : define a controller class and map URLs to handler methods.
@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";
}
}@RestController and @RequestMapping example:
@RestController
@RequestMapping("/demoInfo2")
public class DemoController2 {
@RequestMapping("/test")
public String test() {
return "ok";
}
}@EnableAutoConfiguration : attempts to configure the application automatically based on the jars present on the classpath (e.g., provides an in‑memory HSQLDB if no datasource is defined).
@ComponentScan : automatically discovers beans annotated with @Component, @Service, @Repository, etc., within the package of the annotated class and its sub‑packages.
@Configuration , @Import , @ImportResource : Java‑based configuration alternatives to XML.
@Bean : declares a bean produced by a method.
@Value : injects property values from application.properties.
@Inject : same as @Autowired without required attribute.
@Qualifier : disambiguates injection when multiple beans of the same type exist.
@Resource : similar to @Autowired, defaults to byName injection.
3. JPA Annotations
@Entity (often paired with @Table) marks a class as a JPA entity.
@MappedSuperclass : defines a base class whose properties are inherited by entity subclasses.
@NoRepositoryBean : indicates that a repository interface should not be instantiated.
@Column : maps a field to a database column; can be omitted if names match.
@Id and @GeneratedValue : designate the primary key and its generation strategy.
@SequenceGenerator : defines a database sequence for key generation.
@Transient : excludes a field from persistence.
@JsonIgnore : prevents a property from being serialized to JSON.
@JoinColumn , @OneToOne , @OneToMany , @ManyToOne : define relationships between entities.
4. Spring MVC Annotations
@RequestMapping : maps URLs to controller classes or methods; supports attributes such as params, headers, method, consumes, produces.
@RequestParam : binds request parameters to method arguments.
@PathVariable : binds URI template variables to method arguments.
5. Global Exception Handling
@ControllerAdvice : a component that applies to all controllers for cross‑cutting concerns like exception handling.
@ExceptionHandler : defines a method to handle 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.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
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.
