0
0
Spring Bootframework~5 mins

Returning different status codes in Spring Boot - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
How do you return a 200 OK status with a response body in Spring Boot?
Use ResponseEntity.ok(body) to return a 200 OK status with the given response body.
Click to reveal answer
beginner
What class in Spring Boot helps you customize HTTP status codes in responses?
The <code>ResponseEntity</code> class allows you to set the HTTP status code, headers, and body in your response.
Click to reveal answer
beginner
How can you return a 404 Not Found status when a resource is missing?
Return ResponseEntity.notFound().build() to send a 404 Not Found status without a body.
Click to reveal answer
intermediate
What annotation can you use to set the HTTP status code directly on a controller method?
Use <code>@ResponseStatus(HttpStatus.CODE)</code> on a method or exception class to set the HTTP status code.
Click to reveal answer
intermediate
How do you return a 201 Created status with a location header in Spring Boot?
Use ResponseEntity.created(URI).body(body) to return 201 Created with a Location header pointing to the new resource.
Click to reveal answer
Which Spring Boot class lets you set HTTP status codes in responses?
AHttpServletRequest
BRequestMapping
CRestController
DResponseEntity
How do you return a 404 Not Found status without a body?
Areturn new ResponseEntity<>(HttpStatus.OK);
Breturn ResponseEntity.notFound().build();
Creturn ResponseEntity.ok().build();
Dreturn ResponseEntity.badRequest().build();
Which annotation sets a fixed HTTP status code on a controller method?
A@ResponseStatus
B@RestController
C@GetMapping
D@RequestMapping
What status code is typically returned when a new resource is created?
A200 OK
B404 Not Found
C201 Created
D500 Internal Server Error
How do you include a Location header pointing to a new resource in the response?
AResponseEntity.created(URI).body(body)
BResponseEntity.ok().header("Location", url).body(body)
CResponseEntity.badRequest().build()
DResponseEntity.notFound().build()
Explain how to return different HTTP status codes from a Spring Boot controller method.
Think about ResponseEntity methods and the @ResponseStatus annotation.
You got /5 concepts.
    Describe how to return a 201 Created status with a Location header in Spring Boot.
    Focus on ResponseEntity.created and URI usage.
    You got /4 concepts.