0
0
Spring Bootframework~10 mins

Returning different status codes in Spring Boot - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to return HTTP status 200 OK from a controller method.

Spring Boot
return new ResponseEntity<>("Success", [1]);
Drag options to blanks, or click blank then click option'
AHttpStatus.OK
BHttpStatus.NOT_FOUND
CHttpStatus.BAD_REQUEST
DHttpStatus.INTERNAL_SERVER_ERROR
Attempts:
3 left
💡 Hint
Common Mistakes
Using HttpStatus.NOT_FOUND instead of HttpStatus.OK
Forgetting to specify the status code
2fill in blank
medium

Complete the code to return HTTP status 404 Not Found when a resource is missing.

Spring Boot
return new ResponseEntity<>("Resource not found", [1]);
Drag options to blanks, or click blank then click option'
AHttpStatus.OK
BHttpStatus.NOT_FOUND
CHttpStatus.FORBIDDEN
DHttpStatus.CREATED
Attempts:
3 left
💡 Hint
Common Mistakes
Using HttpStatus.OK instead of HttpStatus.NOT_FOUND
Using HttpStatus.FORBIDDEN which means access denied
3fill in blank
hard

Fix the error in returning HTTP status 201 Created after creating a resource.

Spring Boot
return new ResponseEntity<>(createdObject, [1]);
Drag options to blanks, or click blank then click option'
AHttpStatus.CREATED
BHttpStatus.OK
CHttpStatus.ACCEPTED
DHttpStatus.BAD_REQUEST
Attempts:
3 left
💡 Hint
Common Mistakes
Returning HttpStatus.OK instead of HttpStatus.CREATED
Using HttpStatus.ACCEPTED which means request accepted but not completed
4fill in blank
hard

Fill both blanks to return HTTP 400 Bad Request with a custom error message.

Spring Boot
return new ResponseEntity<>("[1]", [2]);
Drag options to blanks, or click blank then click option'
AInvalid input data
BHttpStatus.BAD_REQUEST
CHttpStatus.UNAUTHORIZED
DOperation failed
Attempts:
3 left
💡 Hint
Common Mistakes
Using a success message with a 400 status
Using HttpStatus.UNAUTHORIZED which means 401 status
5fill in blank
hard

Fill all three blanks to return HTTP 500 Internal Server Error with a detailed message and log the error.

Spring Boot
logger.error("Error occurred: [1]");
return new ResponseEntity<>("[2]", [3]);
Drag options to blanks, or click blank then click option'
Aexception.getMessage()
BInternal server error occurred
CHttpStatus.INTERNAL_SERVER_ERROR
Dexception.toString()
Attempts:
3 left
💡 Hint
Common Mistakes
Logging a generic message instead of the exception message
Returning wrong status codes like 400 or 404