0
0
Spring Bootframework~10 mins

@RestController annotation in Spring Boot - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - @RestController annotation
Start Spring Boot App
Scan for @RestController
Create Controller Bean
Map HTTP Requests to Methods
Execute Method on Request
Return Data as JSON
Send JSON Response to Client
The Spring Boot app starts, finds classes with @RestController, creates them, maps HTTP requests to their methods, executes those methods, and returns JSON responses.
Execution Sample
Spring Boot
@RestController
public class HelloController {
  @GetMapping("/hello")
  public Map<String, String> sayHello() {
    return Map.of("message", "Hello World");
  }
}
Defines a REST controller that responds to GET /hello with JSON {"message":"Hello World"}.
Execution Table
StepActionEvaluationResult
1Start Spring Boot appApp startsSpring context initializes
2Scan classes for @RestControllerFind HelloControllerCreate HelloController bean
3Map HTTP GET /helloMethod sayHello foundMapping stored
4Receive HTTP GET /hello requestMatch to sayHello methodInvoke sayHello()
5Execute sayHello()Return Map.of("message", "Hello World")Response body set to {"message":"Hello World"}
6Send responseResponse body sent as JSONClient receives {"message":"Hello World"}
7No more requestsApp waitsIdle
💡 No more HTTP requests; app remains running waiting for new requests.
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 5Final
HelloController beannullCreated instanceInstance readyInstance method executedInstance ready
HTTP RequestnonenoneGET /hello receivedHandlednone
Response Bodyemptyemptyempty{"message":"Hello World"}{"message":"Hello World"}
Key Moments - 2 Insights
Why does the method return a Map but the client receives JSON?
Because @RestController tells Spring to convert the return value to JSON automatically, as shown in execution_table step 5 where the Map is serialized to JSON for the response body.
What happens if we remove @RestController but keep @GetMapping?
Spring won't create the controller bean or map requests properly, so no response is sent. This is shown in execution_table step 2 where the bean creation depends on @RestController.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what happens at step 3?
AThe app sends the response to the client
BSpring maps the HTTP GET /hello to the sayHello method
CThe HelloController bean is created
DThe app starts running
💡 Hint
Check the 'Action' and 'Result' columns at step 3 in execution_table
At which step does the method sayHello() actually run?
AStep 5
BStep 2
CStep 4
DStep 6
💡 Hint
Look for 'Execute sayHello()' in the 'Action' column in execution_table
If the @RestController annotation is missing, what changes in the execution_table?
AStep 4 would still invoke sayHello()
BStep 5 would return JSON anyway
CStep 2 bean creation would not happen
DStep 6 would send a different response
💡 Hint
Refer to key_moments about bean creation depending on @RestController
Concept Snapshot
@RestController marks a class as a REST API controller.
Spring creates a bean and maps HTTP requests to its methods.
Returned values are automatically converted to JSON.
Use @GetMapping, @PostMapping etc. inside it.
No need for @ResponseBody on each method.
Ideal for building RESTful web services.
Full Transcript
The @RestController annotation in Spring Boot marks a class as a REST API controller. When the app starts, Spring scans for classes with this annotation and creates beans for them. It then maps HTTP requests like GET or POST to the methods inside these classes. When a request comes in, Spring calls the matching method and takes its return value. Because of @RestController, Spring automatically converts this return value to JSON and sends it back as the HTTP response. This makes it easy to build web APIs that return data in JSON format without extra code. For example, a method returning a Map will send that Map serialized as a JSON response. If @RestController is missing, Spring won't create the controller bean or map requests, so the API won't work. This flow helps developers quickly create RESTful services.