Bird
Raised Fist0
Spring Bootframework~20 mins

Problem Details for standard error format in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Problem Details Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What is the output of this Spring Boot error response?
Given a REST controller that throws a ResponseStatusException with status 404 and a custom message, what will the JSON error response contain when using Spring Boot's default Problem Details format?
Spring Boot
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "User not found");
A{"error":"Not Found","message":"User not found","status":404}
B{"type":"about:blank","title":"Not Found","status":404,"detail":"User not found"}
C{"code":404,"error":"User not found"}
D{"status":404,"error":"Not Found","description":"User not found"}
Attempts:
2 left
💡 Hint
Look for the standard keys defined by RFC 7807 in the JSON response.
📝 Syntax
intermediate
2:00remaining
Which code snippet correctly creates a Problem Details response in Spring Boot?
You want to return a custom Problem Details JSON response with status 400 and a detail message. Which code snippet correctly uses Spring Boot's ProblemDetail class?
AProblemDetail pd = new ProblemDetail(400, "Invalid input"); return ResponseEntity.badRequest().body(pd);
BProblemDetail pd = ProblemDetail.create(400, "Invalid input"); return ResponseEntity.status(400).body(pd);
CProblemDetail pd = ProblemDetail.forStatusAndDetail(HttpStatus.BAD_REQUEST, "Invalid input"); return ResponseEntity.status(pd.getStatus()).body(pd);
DProblemDetail pd = ProblemDetail.of(HttpStatus.BAD_REQUEST, "Invalid input"); return ResponseEntity.badRequest().body(pd);
Attempts:
2 left
💡 Hint
Check the official Spring Boot 3 method to create ProblemDetail instances.
🔧 Debug
advanced
2:00remaining
Why does this custom error handler not return Problem Details JSON?
A developer wrote this exception handler method but the response is plain text instead of Problem Details JSON. What is the likely cause?
Spring Boot
@ExceptionHandler(IllegalArgumentException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public String handleBadRequest(IllegalArgumentException ex) {
    return ex.getMessage();
}
AThe method lacks @ResponseBody annotation to serialize the return value.
BThe @ResponseStatus annotation is missing the produces attribute for JSON.
CThe exception handler must return ResponseEntity<ProblemDetail> to produce JSON.
DThe method returns a String, so Spring treats it as plain text instead of ProblemDetail JSON.
Attempts:
2 left
💡 Hint
Consider how Spring decides the response content type based on return type.
🧠 Conceptual
advanced
1:30remaining
What is the purpose of the 'type' field in Problem Details JSON?
In the Problem Details standard error format, what does the type field represent?
AA URI reference that identifies the problem type, providing human-readable documentation.
BThe timestamp when the error occurred.
CA short summary of the error message.
DThe HTTP status code of the error.
Attempts:
2 left
💡 Hint
Think about how clients can learn more about the error type.
❓ state_output
expert
2:30remaining
What is the JSON output of this Spring Boot controller advice method?
Given this controller advice method, what JSON will be returned when a NullPointerException occurs?
Spring Boot
@ControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(NullPointerException.class)
    public ResponseEntity<ProblemDetail> handleNullPointer(NullPointerException ex) {
        ProblemDetail pd = ProblemDetail.forStatusAndDetail(HttpStatus.INTERNAL_SERVER_ERROR, "Null value encountered");
        pd.setType(URI.create("https://example.com/errors/null-pointer"));
        pd.setTitle("Null Pointer Exception");
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(pd);
    }
}
A{"type":"https://example.com/errors/null-pointer","title":"Null Pointer Exception","status":500,"detail":"Null value encountered"}
B{"error":"Null Pointer Exception","message":"Null value encountered","status":500}
C{"status":500,"title":"Null Pointer Exception","description":"Null value encountered"}
D{"type":"about:blank","title":"Internal Server Error","status":500,"detail":"Null value encountered"}
Attempts:
2 left
💡 Hint
Check how the ProblemDetail fields are set explicitly in the method.

Practice

(1/5)
1. What is the main purpose of using Problem Details in Spring Boot APIs?
easy
A. To standardize error messages for better clarity and consistency
B. To speed up API response times
C. To encrypt API responses for security
D. To generate API documentation automatically

Solution

  1. Step 1: Understand the role of Problem Details

    Problem Details format is designed to provide a consistent structure for error messages in APIs.
  2. Step 2: Identify the main benefit

    This standardization helps clients understand errors clearly and handle them properly.
  3. Final Answer:

    To standardize error messages for better clarity and consistency -> Option A
  4. Quick Check:

    Standard error format = clarity [OK]
Hint: Problem Details = clear, consistent error messages [OK]
Common Mistakes:
  • Thinking it improves speed
  • Confusing with security features
  • Assuming it auto-generates docs
2. Which of the following fields is NOT part of the standard Problem Details JSON format?
easy
A. type
B. title
C. timestamp
D. status

Solution

  1. Step 1: Recall standard Problem Details fields

    The standard fields include type, title, status, detail, and instance.
  2. Step 2: Identify the incorrect field

    Timestamp is not part of the official Problem Details specification.
  3. Final Answer:

    timestamp -> Option C
  4. Quick Check:

    Standard fields exclude timestamp [OK]
Hint: Remember standard fields: type, title, status, detail, instance [OK]
Common Mistakes:
  • Confusing timestamp with instance
  • Assuming all error info fields are standard
  • Mixing with custom fields
3. Given this Spring Boot controller snippet, what will be the HTTP status code in the Problem Details response when ResourceNotFoundException is thrown?
@RestController
public class MyController {
  @GetMapping("/item/{id}")
  public Item getItem(@PathVariable String id) {
    throw new ResourceNotFoundException("Item not found");
  }
}
medium
A. 404 Not Found
B. 200 OK
C. 500 Internal Server Error
D. 400 Bad Request

Solution

  1. Step 1: Understand exception mapping in Spring Boot

    ResourceNotFoundException typically maps to HTTP 404 status to indicate missing resource.
  2. Step 2: Confirm standard error status for not found

    Spring Boot's default error handling returns 404 for resource not found exceptions.
  3. Final Answer:

    404 Not Found -> Option A
  4. Quick Check:

    Resource missing = 404 [OK]
Hint: Resource not found usually means 404 status [OK]
Common Mistakes:
  • Assuming 200 OK on exception
  • Confusing 400 with 404
  • Thinking all exceptions return 500
4. You wrote a custom exception handler to return Problem Details but the API returns a plain error message instead. What is the most likely cause?
medium
A. The exception class does not extend RuntimeException
B. The controller method is not annotated with @GetMapping
C. The application.properties file is missing
D. The handler method is missing @ExceptionHandler annotation

Solution

  1. Step 1: Check exception handler setup

    Custom handlers must use @ExceptionHandler to catch exceptions and format responses.
  2. Step 2: Identify missing annotation impact

    Without @ExceptionHandler, Spring Boot won't invoke the handler, so default error response is sent.
  3. Final Answer:

    The handler method is missing @ExceptionHandler annotation -> Option D
  4. Quick Check:

    @ExceptionHandler missing = no custom error [OK]
Hint: Always add @ExceptionHandler to custom error methods [OK]
Common Mistakes:
  • Thinking controller mapping affects exception handling
  • Assuming properties file controls error format
  • Believing exception must extend RuntimeException
5. How can you customize the type field in a Problem Details response for a specific exception in Spring Boot?
hard
A. Override the toString() method of the exception class
B. Create a @ControllerAdvice with @ExceptionHandler and build a ProblemDetail with custom type URI
C. Set a property in application.properties named error.type.custom
D. Add a @RequestMapping with produces = "application/problem+json" on the controller

Solution

  1. Step 1: Use @ControllerAdvice and @ExceptionHandler

    To customize error details, define a global handler that catches the exception.
  2. Step 2: Build ProblemDetail with custom type URI

    Inside the handler, create a ProblemDetail object and set the type field to a custom URI describing the error.
  3. Final Answer:

    Create a @ControllerAdvice with @ExceptionHandler and build a ProblemDetail with custom type URI -> Option B
  4. Quick Check:

    Custom type = @ExceptionHandler + ProblemDetail [OK]
Hint: Use @ExceptionHandler to customize ProblemDetail fields [OK]
Common Mistakes:
  • Trying to override toString() for error format
  • Expecting properties file to set type field
  • Using @RequestMapping alone to customize error
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard