Spring Boot makes it super easy to build RESTful web services in Java. If you’re working on any kind of backend application — like a blog, e-commerce site, or mobile app backend — chances are you’ll need to build a REST API.
In this article, I’ll explain what REST APIs are and how to create one using Spring Boot — with clear examples and no unnecessary theory.
🔍 What is a REST API?
REST stands for Representational State Transfer. It’s an architectural style for building APIs that work over HTTP.
A REST API allows your application to Create, Read, Update, and Delete data using HTTP methods like:
HTTP Method | Purpose |
---|---|
GET | Read data |
POST | Create data |
PUT | Update data |
DELETE | Delete data |
🚀 Getting Started with Spring Boot REST API
✅ Step 1: Create a Spring Boot Project
You can create a Spring Boot project using Spring Initializr.
Choose:
- Spring Web (for REST APIs)
- Spring Boot DevTools (optional for hot reload)
- Spring Data JPA and H2 Database (for database access)
Download and import it into your IDE (like IntelliJ or Eclipse).
✅ Step 2: Create a Model Class
Let’s say we’re building a simple User
API.
package com.example.demo.model;
public class User {
private Long id;
private String name;
private String email;
// Getters and Setters
}
✅ Step 3: Create a REST Controller
This is where we define the API endpoints.
package com.example.demo.controller;
import com.example.demo.model.User;
import org.springframework.web.bind.annotation.*;
import java.util.*;
@RestController
@RequestMapping("/api/users")
public class UserController {
private List<User> users = new ArrayList<>();
@GetMapping
public List<User> getAllUsers() {
return users;
}
@PostMapping
public User createUser(@RequestBody User user) {
users.add(user);
return user;
}
@GetMapping("/{id}")
public User getUserById(@PathVariable Long id) {
return users.stream().filter(u -> u.getId().equals(id)).findFirst().orElse(null);
}
@PutMapping("/{id}")
public User updateUser(@PathVariable Long id, @RequestBody User updatedUser) {
for (User user : users) {
if (user.getId().equals(id)) {
user.setName(updatedUser.getName());
user.setEmail(updatedUser.getEmail());
return user;
}
}
return null;
}
@DeleteMapping("/{id}")
public String deleteUser(@PathVariable Long id) {
users.removeIf(u -> u.getId().equals(id));
return "User deleted successfully";
}
}
✅ That’s it — you’ve just built a basic REST API in Spring Boot!
🧪 Test Your API
You can use tools like:
Or even test directly in the browser using http://localhost:8080/api/users
.
💡 Real-World Tip
In a real application, you’d use:
- Database + JPA Repository instead of a List
- DTOs for clean API communication
- Validation with
@Valid
- Service Layer for business logic
- Exception handling using
@ControllerAdvice
📌 Summary
With Spring Boot, creating a REST API is super simple and fast. You just need:
- A model class
- A REST controller
- Optional: service and database layer
This structure is great for building everything from small tools to large enterprise apps.
🔗 You May Also Like
👉 Asynchronous JavaScript: Promises, Async/Await, and Callbacks
📎 External Resource
Arsalan Malik is a passionate Software Engineer and the Founder of Makemychance.com. A proud CDAC-qualified developer, Arsalan specializes in full-stack web development, with expertise in technologies like Node.js, PHP, WordPress, React, and modern CSS frameworks.
He actively shares his knowledge and insights with the developer community on platforms like Dev.to and engages with professionals worldwide through LinkedIn.
Arsalan believes in building real-world projects that not only solve problems but also educate and empower users. His mission is to make technology simple, accessible, and impactful for everyone.