ResponseStatusException with status 404 and a custom message, what will the JSON error response contain when using Spring Boot's default Problem Details format?throw new ResponseStatusException(HttpStatus.NOT_FOUND, "User not found");Spring Boot uses the Problem Details format (RFC 7807) by default for error responses. It includes keys like type, title, status, and detail.
ProblemDetail class?The ProblemDetail.forStatusAndDetail static method is the correct way to create a ProblemDetail with status and detail.
@ExceptionHandler(IllegalArgumentException.class) @ResponseStatus(HttpStatus.BAD_REQUEST) public String handleBadRequest(IllegalArgumentException ex) { return ex.getMessage(); }
Returning a String causes Spring to treat the response as plain text. To return Problem Details JSON, the method should return a ProblemDetail object or ResponseEntity<ProblemDetail>.
type field represent?The type field is a URI that points to documentation about the error type, helping clients understand the problem.
NullPointerException occurs?@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); } }
The method sets a custom type URI and title, so the JSON reflects those values along with status and detail.
