REST APIs in Spring Boot are typically created using the @RestController annotation, which exposes application functionality through HTTP endpoints. Spring Boot provides annotations for handling different HTTP methods, processing request data, and returning responses that are automatically converted into JSON or XML.
Key Points: • @RestController is used to create REST API endpoints. • Mapping annotations such as @GetMapping and @PostMapping handle HTTP requests. • Spring Boot automatically converts Java objects into JSON responses. • Business logic is usually placed in the Service layer. • REST APIs commonly support CRUD (Create, Read, Update, Delete) operations.
Steps to Create a REST API
1. Create a Controller
A controller receives client requests and returns responses.
Code Example:
@RestController
@RequestMapping("/employees")
public class EmployeeController {
}2. Define API Endpoints
Spring Boot provides dedicated annotations for HTTP methods.
GET
Retrieve data.
@PostMapping
Create data.
@PutMapping
Update data.
@DeleteMapping
Delete data.Example:
@RestController
@RequestMapping("/employees")
public class EmployeeController {
@GetMapping
public String getEmployees() {
return "Employee List";
}
}3. Accept Request Data
Use @RequestBody to receive JSON input.
Code Example:
@PostMapping
public Employee createEmployee(
@RequestBody Employee employee) {
return employee;
}Request:
{ "id": 101, "name": "John" }
Spring automatically converts JSON into a Java object.
4. Handle Path Variables
Use @PathVariable to read values from the URL.
Code Example:
@GetMapping("/{id}")
public String getEmployee(
@PathVariable Long id) {
return "Employee Id: " + id;
}Request:
GET /employees/101
5. Handle Query Parameters
Use @RequestParam to read query parameters.
Code Example:
@GetMapping("/search")
public String searchEmployee(
@RequestParam String name) {
return name;
}Request:
GET /employees/search?name=John
6. Call Service Layer
Controllers should delegate business logic to service classes.
Code Example:
@RestController
@RequestMapping("/employees")
public class EmployeeController {
private final EmployeeService service;
public EmployeeController(
EmployeeService service) {
this.service = service;
}
@GetMapping("/{id}")
public Employee getEmployee(
@PathVariable Long id) {
return service.getEmployee(id);
}
}This keeps the application clean and maintainable.
How a REST API Request Flows
Client Request | @RestController | Service Layer | Repository Layer | Database | JSON Response
Spring Boot manages most of this process automatically.
Complete CRUD Example
Code Example:
@RestController
@RequestMapping("/employees")
public class EmployeeController {
@GetMapping
public List<Employee> getAllEmployees() {
return employeeService.getAllEmployees();
}
@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);
}
}Spring Boot automatically converts returned Java objects into JSON responses.
Example: Suppose an Employee Management System exposes:
GET /employees
Returns all employees.
GET /employees/101
Returns employee details.
POST /employees
Creates a new employee.
PUT /employees/101
Updates employee information.
DELETE /employees/101
Deletes an employee.
These endpoints together form a complete REST API.
Benefits of Spring Boot REST APIs
• Minimal configuration • Automatic JSON conversion • Embedded server support • Easy integration with databases • Rapid API development • Excellent support for microservices
Real-World Example
In an e-commerce application, REST APIs can be used for:
• Product Management • Order Processing • Customer Management • Payment Services
Clients such as web applications, mobile applications, and third-party systems interact with these APIs through HTTP requests.
Interview Tip: A concise interview answer is:
"To create REST APIs in Spring Boot, we use @RestController to define REST endpoints and mapping annotations such as @GetMapping, @PostMapping, @PutMapping, and @DeleteMapping to handle HTTP requests. Request data can be received using @RequestBody, @PathVariable, and @RequestParam. Business logic is delegated to service classes, and Spring Boot automatically converts Java objects into JSON responses."