Problem Details provide a simple, consistent way to show errors in web APIs. It helps clients understand what went wrong without confusion.
Problem Details for standard error format in Spring Boot
HTTP/1.1 400 Bad Request Content-Type: application/problem+json { "type": "https://example.com/probs/out-of-credit", "title": "You do not have enough credit.", "status": 400, "detail": "Your current balance is 30, but that costs 50.", "instance": "/account/12345/transactions/abc" }
The Content-Type must be application/problem+json to follow the standard.
Fields like type, title, status, detail, and instance help describe the error clearly.
{
"type": "https://example.com/probs/out-of-credit",
"title": "You do not have enough credit.",
"status": 400,
"detail": "Your current balance is 30, but that costs 50.",
"instance": "/account/12345/transactions/abc"
}{
"type": "https://example.com/probs/not-found",
"title": "Resource not found.",
"status": 404,
"detail": "The requested item was not found in the database.",
"instance": "/items/999"
}This Spring Boot controller returns a problem detail error when the item is not found. It uses the standard format to describe the error.
package com.example.demo; import org.springframework.http.HttpStatus; import org.springframework.http.ProblemDetail; 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; import java.net.URI; @RestController public class ItemController { @GetMapping("/items/{id}") public ResponseEntity<?> getItem(@PathVariable int id) { if (id != 1) { ProblemDetail problem = ProblemDetail.forStatus(HttpStatus.NOT_FOUND); problem.setType(URI.create("https://example.com/probs/not-found")); problem.setTitle("Resource not found."); problem.setDetail("Item with id " + id + " was not found."); problem.setInstance(URI.create("/items/" + id)); return ResponseEntity.status(HttpStatus.NOT_FOUND).body(problem); } return ResponseEntity.ok("Item 1 details"); } }
Spring Boot 3+ supports ProblemDetail class to create standard error responses easily.
Always set the Content-Type header to application/problem+json for clients to recognize the format.
Use meaningful type URIs to help clients find more info about the error.
Problem Details standardize error messages in APIs for clarity.
Use fields like type, title, status, detail, and instance.
Spring Boot provides built-in support to create these error responses easily.
Practice
Solution
Step 1: Understand the role of Problem Details
Problem Details format is designed to provide a consistent structure for error messages in APIs.Step 2: Identify the main benefit
This standardization helps clients understand errors clearly and handle them properly.Final Answer:
To standardize error messages for better clarity and consistency -> Option AQuick Check:
Standard error format = clarity [OK]
- Thinking it improves speed
- Confusing with security features
- Assuming it auto-generates docs
Solution
Step 1: Recall standard Problem Details fields
The standard fields include type, title, status, detail, and instance.Step 2: Identify the incorrect field
Timestamp is not part of the official Problem Details specification.Final Answer:
timestamp -> Option CQuick Check:
Standard fields exclude timestamp [OK]
- Confusing timestamp with instance
- Assuming all error info fields are standard
- Mixing with custom fields
ResourceNotFoundException is thrown?
@RestController
public class MyController {
@GetMapping("/item/{id}")
public Item getItem(@PathVariable String id) {
throw new ResourceNotFoundException("Item not found");
}
}Solution
Step 1: Understand exception mapping in Spring Boot
ResourceNotFoundException typically maps to HTTP 404 status to indicate missing resource.Step 2: Confirm standard error status for not found
Spring Boot's default error handling returns 404 for resource not found exceptions.Final Answer:
404 Not Found -> Option AQuick Check:
Resource missing = 404 [OK]
- Assuming 200 OK on exception
- Confusing 400 with 404
- Thinking all exceptions return 500
Solution
Step 1: Check exception handler setup
Custom handlers must use @ExceptionHandler to catch exceptions and format responses.Step 2: Identify missing annotation impact
Without @ExceptionHandler, Spring Boot won't invoke the handler, so default error response is sent.Final Answer:
The handler method is missing @ExceptionHandler annotation -> Option DQuick Check:
@ExceptionHandler missing = no custom error [OK]
- Thinking controller mapping affects exception handling
- Assuming properties file controls error format
- Believing exception must extend RuntimeException
type field in a Problem Details response for a specific exception in Spring Boot?Solution
Step 1: Use @ControllerAdvice and @ExceptionHandler
To customize error details, define a global handler that catches the exception.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.Final Answer:
Create a @ControllerAdvice with @ExceptionHandler and build a ProblemDetail with custom type URI -> Option BQuick Check:
Custom type = @ExceptionHandler + ProblemDetail [OK]
- Trying to override toString() for error format
- Expecting properties file to set type field
- Using @RequestMapping alone to customize error
Ready to prove it?
Pass the test and this pattern moves unknown
