Challenge - 5 Problems
Status Code Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What HTTP status code does this Spring Boot controller return?
Consider this Spring Boot controller method. What status code will the client receive when calling this endpoint?
Spring Boot
import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class MyController { @GetMapping("/check") public ResponseEntity<String> check() { return ResponseEntity.status(201).body("Created"); } }
Attempts:
2 left
💡 Hint
Look at the status method used in ResponseEntity.
✗ Incorrect
The method uses ResponseEntity.status(201) which sets the HTTP status code to 201 Created.
📝 Syntax
intermediate2:00remaining
Which option correctly returns a 404 status with a message in Spring Boot?
You want to return a 404 Not Found status with a message "User not found". Which code snippet is correct?
Attempts:
2 left
💡 Hint
Check the ResponseEntity methods and constructors.
✗ Incorrect
Option A correctly uses ResponseEntity.status(404).body(...) to set status and body. Option A is invalid because notFound() returns a ResponseEntity without a body() method. Option A is invalid syntax as there is no status(HttpStatus, Object) method. Option A uses invalid constructor arguments (expects HttpStatus, not int).
🔧 Debug
advanced2:00remaining
Why does this Spring Boot controller always return 200 OK instead of 400 Bad Request?
Look at this controller method. It tries to return 400 Bad Request on error but always returns 200 OK. Why?
Spring Boot
import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class ErrorController { @GetMapping("/error") public ResponseEntity<String> error(boolean hasError) { if (hasError) { return ResponseEntity.status(400).body("Bad Request"); } return ResponseEntity.ok("All good"); } }
Attempts:
2 left
💡 Hint
Check if the method returns the ResponseEntity created inside the if block.
✗ Incorrect
The method creates a ResponseEntity with status 400 but does not return it. The method always returns ResponseEntity.ok("All good"), so status 200 is sent.
❓ state_output
advanced2:00remaining
What is the HTTP status code when this Spring Boot controller returns null body with 204 status?
This controller returns ResponseEntity with status 204 No Content and a null body. What will the client receive?
Spring Boot
import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class NoContentController { @GetMapping("/nocontent") public ResponseEntity<String> noContent() { return ResponseEntity.noContent().build(); } }
Attempts:
2 left
💡 Hint
Check what ResponseEntity.noContent().build() does.
✗ Incorrect
ResponseEntity.noContent().build() returns a response with status 204 and no body content, so the client receives 204 No Content with empty body.
🧠 Conceptual
expert3:00remaining
Which option correctly demonstrates returning different status codes based on input in Spring Boot?
You want a controller method that returns 200 OK with "Success" if input is true, and 404 Not Found with "Not Found" if false. Which code snippet correctly implements this?
Attempts:
2 left
💡 Hint
Check which options correctly set both status and body for 404 response.
✗ Incorrect
Option B correctly returns 200 OK with body and 404 with body using fluent API. Option B is valid: ResponseEntity.notFound().build() returns 404 without body. Option B works but uses constructor style (requires HttpStatus import). Option B is invalid: notFound() has no body() method.