0
0
Spring Bootframework~10 mins

Returning different status codes in Spring Boot - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Returning different status codes
Client sends HTTP request
Controller method receives request
Process request logic
Decide status code to return
Return 200
Client receives response with status code
The server receives a request, processes it, decides which HTTP status code fits the result, and sends that code back to the client.
Execution Sample
Spring Boot
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyController {

  @GetMapping("/status/{code}")
  public ResponseEntity<String> getStatus(@PathVariable int code) {
    return switch (code) {
      case 200 -> ResponseEntity.ok("OK");
      case 404 -> ResponseEntity.status(404).body("Not Found");
      case 500 -> ResponseEntity.status(500).body("Server Error");
      default -> ResponseEntity.badRequest().body("Invalid code");
    };
  }
}
This controller method returns different HTTP status codes and messages based on the input code.
Execution Table
StepInput codeCondition matchedResponseEntity statusResponse body
1200case 200200 OK"OK"
2404case 404404 Not Found"Not Found"
3500case 500500 Server Error"Server Error"
4123default400 Bad Request"Invalid code"
5-No more inputs--
💡 Execution stops after returning the response for the given input code.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
codeN/A200404500123123
ResponseEntity.statusN/A200404500400400
ResponseEntity.bodyN/A"OK""Not Found""Server Error""Invalid code""Invalid code"
Key Moments - 2 Insights
Why does the method return different status codes instead of always 200?
Because the method uses a switch on the input code to decide which HTTP status to send back, as shown in execution_table rows 1-4.
What happens if the input code is not 200, 404, or 500?
The default case runs, returning a 400 Bad Request status with message "Invalid code", as seen in execution_table row 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the ResponseEntity status when input code is 404?
A500 Server Error
B200 OK
C404 Not Found
D400 Bad Request
💡 Hint
Check execution_table row 2 under 'ResponseEntity status'
At which step does the method return a 400 Bad Request status?
AStep 1
BStep 4
CStep 3
DStep 2
💡 Hint
Look at execution_table row 4 for the default case
If the input code is changed to 200, what will be the response body?
A"OK"
B"Server Error"
C"Not Found"
D"Invalid code"
💡 Hint
Refer to execution_table row 1 under 'Response body'
Concept Snapshot
In Spring Boot, use ResponseEntity to return different HTTP status codes.
Use ResponseEntity.ok() for 200 OK.
Use ResponseEntity.status(code).body(message) for other codes.
Switch or if-else decides which status to send.
This helps client know result type clearly.
Full Transcript
This example shows how a Spring Boot controller method returns different HTTP status codes based on input. The method uses a switch statement on the input code. For 200, it returns ResponseEntity.ok with message "OK". For 404, it returns status 404 with "Not Found". For 500, it returns status 500 with "Server Error". For any other code, it returns 400 Bad Request with "Invalid code". The execution table traces each input and the response status and body returned. The variable tracker shows how the input code and response values change step by step. Key moments clarify why different codes are returned and what happens on unknown codes. The quiz tests understanding of which status and body correspond to which input code. This helps beginners see how to control HTTP responses clearly in Spring Boot.