Spring Boot Annotations You Must Know for REST APIs

Spring Boot Annotations You Must Know for REST APIs

Spring Boot simplifies Java backend development by providing a rich set of annotations that help build REST APIs efficiently. In this guide, we’ll explore the most important annotations you need to know and how they work together to form robust backend services.

@SpringBootApplication

This is a composite annotation that combines:

  • @Configuration: Marks the class as a source of bean definitions.
  • @EnableAutoConfiguration: Enables Spring Boot's auto-configuration mechanism.
  • @ComponentScan: Enables component scanning in the package and subpackages.

It is placed on the main class to bootstrap the application.

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

@RestController

This annotation is a convenience annotation that combines @Controller and @ResponseBody. It tells Spring that the class is a controller where every method returns a domain object instead of a view.

@RestController
@RequestMapping("/api/employees")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @GetMapping
    public List getAllEmployees() {
        return employeeService.getAll();
    }
}

@RequestMapping

Used to map web requests to Spring controller methods. It can be used on both class and method levels.

@RequestMapping(value = "/api", method = RequestMethod.GET)

@GetMapping / @PostMapping / @PutMapping / @DeleteMapping

These are shortcut annotations for @RequestMapping with specific HTTP methods.

@GetMapping("/{id}")
public Employee getEmployeeById(@PathVariable Long id) {
    return employeeService.getById(id);
}

@PostMapping
public Employee createEmployee(@RequestBody Employee employee) {
    return employeeService.save(employee);
}

@PutMapping("/{id}")
public Employee updateEmployee(@PathVariable Long id, @RequestBody Employee employee) {
    return employeeService.update(id, employee);
}

@DeleteMapping("/{id}")
public void deleteEmployee(@PathVariable Long id) {
    employeeService.delete(id);
}

@PathVariable

Binds a method parameter to a URI template variable.

@GetMapping("/{id}")
public Employee getById(@PathVariable Long id) {
    return service.get(id);
}

@RequestBody

Automatically binds the incoming JSON request body to a Java object.

@PostMapping
public Employee create(@RequestBody Employee employee) {
    return service.save(employee);
}

@Autowired

Marks a constructor, field, or method to be autowired by Spring’s dependency injection.

@Autowired
private EmployeeService service;

@Service, @Repository, @Component

  • @Service: Used on service layer classes.
  • @Repository: Used for DAO classes. It also enables exception translation.
  • @Component: A generic stereotype for any Spring-managed component.
@Service
public class EmployeeService {
    // Business logic here
}

@Entity and @Table

Used in JPA to define a database table.

@Entity
@Table(name = "employees")
public class Employee {
    @Id
    private Long id;

    private String name;

    private String email;
}

Conclusion

Mastering these annotations is essential for building robust REST APIs with Spring Boot. These annotations help you define routes, bind data, and inject dependencies with ease, allowing you to focus on business logic instead of boilerplate configuration.

🔗 Related Posts

🛠️ CRUD API with Oracle DB

A full REST API example showing use of key Spring annotations.

🔍 @RestController vs @Controller

Understand the difference between annotation-based controller types.

🚨 Global Exception Handling

Use @ControllerAdvice and @ExceptionHandler to catch and return custom error responses.

📦 Return JSON Responses

Explore how to return clean and structured JSON responses using annotations.