Spring Boot Annotations Explained for Freshers (REST API Focus)

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.

This guide helps freshers:
  • 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);
  }
}
Interview tip: Component scanning starts from the package of this class and goes downward.

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();
  }
}
Interview classic: Use @RestController for APIs, @Controller for MVC views.

Read a detailed comparison here: @RestController vs @Controller.


@RequestMapping vs @GetMapping / @PostMapping

AnnotationUsage
@RequestMappingGeneric mapping (any HTTP method)
@GetMappingRead data
@PostMappingCreate data
@PutMappingUpdate data
@DeleteMappingDelete 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;
Best practice: Prefer constructor injection — safer and easier to test.
public EmployeeController(EmployeeService service) {
  this.service = service;
}

@Component, @Service, @Repository

AnnotationPurpose
@ComponentGeneric Spring-managed bean
@ServiceBusiness logic layer
@RepositoryDatabase layer + exception translation
Interview insight: @Repository converts database exceptions into Spring exceptions.

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


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.