Spring Boot Annotations Explained for Freshers (REST API Focus)
Spring Boot annotations define how your application starts, handles HTTP requests, injects dependencies, and talks to the database. Understanding these annotations conceptually is far more important than memorizing them — especially for interviews.
- Understand annotations by application layer
- Know WHY an annotation is used
- Avoid common mistakes interviewers notice
- Connect annotations to real REST API projects
1️⃣ Application Bootstrapping
@SpringBootApplication
This annotation marks the starting point of a Spring Boot application. It internally combines:
- @Configuration – allows defining beans
- @EnableAutoConfiguration – configures components automatically
- @ComponentScan – scans Spring beans
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
If you want to understand what Spring exposes at runtime, see Spring Boot Actuator explained.
2️⃣ REST Controller Annotations
@RestController
Marks a class as a REST controller.
It is equivalent to using @Controller + @ResponseBody.
All methods return JSON by default.
@RestController
@RequestMapping("/api/employees")
public class EmployeeController {
private final EmployeeService service;
public EmployeeController(EmployeeService service) {
this.service = service;
}
@GetMapping
public List<Employee> getAll() {
return service.getAll();
}
}
Read a detailed comparison here: @RestController vs @Controller.
@RequestMapping vs @GetMapping / @PostMapping
| Annotation | Usage |
|---|---|
| @RequestMapping | Generic mapping (any HTTP method) |
| @GetMapping | Read data |
| @PostMapping | Create data |
| @PutMapping | Update data |
| @DeleteMapping | Delete data |
Shortcut annotations improve readability and are preferred in real projects.
@PathVariable & @RequestBody
@GetMapping("/{id}")
public Employee getById(@PathVariable Long id) {
return service.get(id);
}
@PostMapping
public Employee create(@RequestBody Employee employee) {
return service.save(employee);
}
- @PathVariable → data from URL
- @RequestBody → JSON to Java object
To return structured responses properly, see Returning JSON responses in Spring Boot.
3️⃣ Dependency Injection Annotations
@Autowired (and the better approach)
@Autowired injects a required dependency automatically.
@Autowired
private EmployeeService service;
public EmployeeController(EmployeeService service) {
this.service = service;
}
@Component, @Service, @Repository
| Annotation | Purpose |
|---|---|
| @Component | Generic Spring-managed bean |
| @Service | Business logic layer |
| @Repository | Database layer + exception translation |
4️⃣ JPA & Database Annotations
@Entity, @Table, @Id
@Entity
@Table(name = "employees")
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String email;
}
This maps a Java class to a database table using JPA.
If you are new to JPA, read: Spring Boot + JPA Basics for Freshers.
5️⃣ How All These Annotations Work Together
HTTP Request ↓ @RestController ↓ @Service ↓ @Repository ↓ JPA / Hibernate ↓ Database
Annotations define responsibilities — not just syntax. This layered approach improves maintainability and performance.
6️⃣ Common Fresher Mistakes (Interview Gold)
- Using @Component instead of @Service everywhere
- Injecting Repository directly into Controller
- Returning Entity objects directly in APIs
- Not handling exceptions globally
For proper error handling, see Global Exception Handling in Spring Boot.
7️⃣ Interview Preparation Links
- Top 50 Spring Boot Interview Questions for Freshers
- Spring Boot Interview Questions (2–5 Years)
- Advanced Spring Boot & Microservices (Senior)
Final Summary
- Annotations define how Spring Boot applications behave
- Understanding WHY matters more than memorizing names
- Layered usage of annotations is key for interviews
If you understand these annotations clearly, you can confidently build REST APIs and answer fresher-level Spring Boot interviews.