CRUD APIs using Spring Boot
All Java Topics
Last updated: May 25, 2026
Author: ManaCoding Team
CRUD APIs in Spring Boot allow you to Create, Read, Update, and Delete data using REST endpoints. It typically uses Controller, Service, and Repository layers with Spring Data JPA.
Syntax
@RestController
@RequestMapping("/api/users")
class UserController {
}
Example Program
import org.springframework.web.bind.annotation.*;
import java.util.*;
@RestController
@RequestMapping("/api/users")
class UserController {
private List<String> users = new ArrayList<>(Arrays.asList("John", "Alex"));
// READ ALL
@GetMapping
public List<String> getAllUsers() {
return users;
}
// READ BY ID
@GetMapping("/{id}")
public String getUser(@PathVariable int id) {
return users.get(id);
}
// CREATE
@PostMapping
public String createUser(@RequestBody String user) {
users.add(user);
return "User Created";
}
// UPDATE
@PutMapping("/{id}")
public String updateUser(@PathVariable int id, @RequestBody String user) {
users.set(id, user);
return "User Updated";
}
// DELETE
@DeleteMapping("/{id}")
public String deleteUser(@PathVariable int id) {
users.remove(id);
return "User Deleted";
}
}
// Output examples:
// GET /api/users
// POST /api/users
// PUT /api/users/1
// DELETE /api/users/1
What is CRUD?
- 1 Create – add new data
- 2 Read – retrieve data
- 3 Update – modify data
- 4 Delete – remove data
Spring Boot CRUD Flow
- 1 Client sends HTTP request.
- 2 Controller handles request.
- 3 Service processes logic.
- 4 Repository interacts with database.
HTTP Methods Used
- 1 POST – Create
- 2 GET – Read
- 3 PUT – Update
- 4 DELETE – Delete
Why CRUD APIs?
- 1 Core of backend development.
- 2 Used in all web systems.
- 3 Easy integration with frontend.
- 4 Standard REST practice.
Quick Summary
- CRUD APIs handle basic data operations.
- Spring Boot simplifies REST CRUD development.
- Uses Controller-Service-Repository structure.
- Follows standard HTTP methods.
FAQs
What is CRUD in REST API?
Create, Read, Update, and Delete operations on data.
Which annotation is used for REST controller?
@RestController.
Which method is used to update data?
PUT method.
What is used for reading data?
GET method.
Why use CRUD APIs?
To manage data operations in applications.
Interview Questions
Q1.
What is CRUD in REST API?
Answer:
Create, Read, Update, and Delete operations on data.
Q2.
Which annotation is used for REST controller?
Answer:
@RestController.
Q3.
Which method is used to update data?
Answer:
PUT method.
Q4.
What is used for reading data?
Answer:
GET method.
Q5.
Why use CRUD APIs?
Answer:
To manage data operations in applications.
Quiz
Which HTTP method is used for deleting data in CRUD?