Spring Boot RESTful API – Complete
Explanation with Example
A RESTful API (Representational State Transfer API) is a web service that allows
applications to communicate over HTTP. In Spring Boot, creating REST APIs is simple
using annotations such as @RestController, @GetMapping, @PostMapping, etc.
Why REST API?
Suppose you have a Student Registration System.
Mobile App → Requests student information.
Web Application → Adds new students.
Another Application → Updates student records.
Instead of accessing the database directly, all applications communicate through a REST
API.
+----------------+
| Mobile App |
+----------------+
|
|
+----------------+
| Web Application|
+----------------+
|
| HTTP Request
v
+------------------------+
| Spring Boot REST API |
+------------------------+
|
|
+----------------+
| MySQL Database |
+----------------+
HTTP Methods
Method Purpose Example URL
GET Read Data /students
POST Insert Data /students
PUT Update Data /students/101
DELETE Delete Data /students/101
Below is a complete StudentController class containing all basic RESTful operations:
✅ Select All Students
✅ Select Student by ID
✅ Insert Student
✅ Update Student
✅ Delete Student
This example uses an ArrayList instead of a database, making it ideal for beginners.
package [Link];
import [Link];
import [Link];
import [Link].*;
import [Link];
@RestController
public class StudentController {
// ArrayList acts as our database
List<Student> students = new ArrayList<>();
// Constructor
public StudentController() {
[Link](new Student(101, "Ravi", "Java", 85));
[Link](new Student(102, "Priya", "Python", 90));
[Link](new Student(103, "Arun", "Spring Boot", 88));
}
// ==========================================
// SELECT ALL STUDENTS
// URL : GET [Link]
// ==========================================
@GetMapping("/students")
public List<Student> getAllStudents() {
return students;
}
// ==========================================
// SELECT STUDENT BY ID
// URL : GET [Link]
// ==========================================
@GetMapping("/students/{id}")
public Student getStudent(@PathVariable int id) {
for (Student s : students) {
if ([Link]() == id) {
return s;
}
}
return null;
}
// ==========================================
// INSERT STUDENT
// URL : POST [Link]
// ==========================================
@PostMapping("/students")
public String addStudent(@RequestBody Student student) {
[Link](student);
return "Student Added Successfully";
}
// ==========================================
// UPDATE STUDENT
// URL : PUT [Link]
// ==========================================
@PutMapping("/students")
public String updateStudent(@RequestBody Student student) {
for (Student s : students) {
if ([Link]() == [Link]()) {
[Link]([Link]());
[Link]([Link]());
[Link]([Link]());
return "Student Updated Successfully";
}
}
return "Student Not Found";
}
// ==========================================
// DELETE STUDENT
// URL : DELETE [Link]
// ==========================================
@DeleteMapping("/students/{id}")
public String deleteStudent(@PathVariable int id) {
for (Student s : students) {
if ([Link]() == id) {
[Link](s);
return "Student Deleted Successfully";
}
}
return "Student Not Found";
}
[Link]
package [Link];
public class Student {
private int id;
private String name;
private String course;
private double marks;
public Student() {
public Student(int id, String name, String course, double marks) {
[Link] = id;
[Link] = name;
[Link] = course;
[Link] = marks;
}
public int getId() {
return id;
}
public void setId(int id) {
[Link] = id;
}
public String getName() {
return name;
}
public void setName(String name) {
[Link] = name;
}
public String getCourse() {
return course;
}
public void setCourse(String course) {
[Link] = course;
}
public double getMarks() {
return marks;
}
public void setMarks(double marks) {
[Link] = marks;
}
REST API Endpoints
HTTP Method URL Description
GET [Link] Get all students
GET [Link] Get a student by ID
POST [Link] Add a new student
PUT [Link] Update an existing student
DELETE [Link] Delete a student by ID
Sample JSON for POST
{
"id":104,
"name":"Kumar",
"course":"Data Science",
"marks":92
}
Sample JSON for PUT
{
"id":104,
"name":"Kumar Raj",
"course":"AI & ML",
"marks":96
}