DEV Community

Likitha Chendrimada Suguna
Likitha Chendrimada Suguna

Posted on

What Really Happens When You Call a REST API in Spring Boot?

If you are learning Spring Boot, you have probably written something like this:

@GetMapping("/users")
public List<User> getUsers() {
    return userService.getUsers();
}
Enter fullscreen mode Exit fullscreen mode

It looks simple. But what actually happens when a client calls /users?

Let's understand it in a simple way.

1. The Client Sends a Request

Suppose your frontend, Postman, or mobile app sends:

GET /users
Enter fullscreen mode Exit fullscreen mode

The request reaches your Spring Boot application.

2. The Controller Receives It

Spring Boot looks for a controller that matches the URL and HTTP method.

@RestController
public class UserController {

    @GetMapping("/users")
    public List<User> getUsers() {
        return userService.getUsers();
    }
}
Enter fullscreen mode Exit fullscreen mode

The controller's job is mainly to receive the request and send back a response.

3. The Service Does the Work

The controller usually doesn't contain all the business logic.

It calls the service:

public List<User> getUsers() {
    return userRepository.findAll();
}
Enter fullscreen mode Exit fullscreen mode

The service layer is where we usually keep our application logic.

4. The Repository Talks to the Database

The repository communicates with the database.

userRepository.findAll();
Enter fullscreen mode Exit fullscreen mode

Spring Data JPA handles much of the database work for us.

The database returns the user data.

5. Spring Boot Sends the Response

The data travels back:

Database
   ↓
Repository
   ↓
Service
   ↓
Controller
   ↓
Client
Enter fullscreen mode Exit fullscreen mode

Spring Boot converts the Java objects into JSON.

For example:

[
  {
    "id": 1,
    "name": "Likitha"
  }
]
Enter fullscreen mode Exit fullscreen mode

The Simple Picture

Whenever you call a REST API, think of it like this:

Client
  ↓
Controller
  ↓
Service
  ↓
Repository
  ↓
Database
  ↓
Repository
  ↓
Service
  ↓
Controller
  ↓
Response
Enter fullscreen mode Exit fullscreen mode

That's the basic flow behind many Spring Boot applications.

At first, these layers can feel confusing. But once you understand who does what, Spring Boot becomes much easier to work with.


💡 If you're learning Java backend development, understanding this flow is more important than simply memorizing annotations.

Top comments (0)