Standard Error Response

A standard error response is one fixed JSON structure that your API returns for every kind of failure, whether it is a missing record, invalid input, or a server problem. It is a small class (here ErrorResponse) holding the same fields each time: status, message, timestamp, and request path. It is used so that front-end and mobile developers can write one piece of error-handling code, and so that logs and API documentation stay consistent. In Spring Boot, the @RestControllerAdvice class builds this object for every exception, so no endpoint returns a different error format.

Features

  • One ErrorResponse class used for all error types
  • Contains status, message, timestamp, and path
  • The same structure for 400, 404, 405, 409, and 500 errors
  • Validation errors are combined into one readable message
  • Handles Spring’s built-in errors (broken JSON, wrong URL type, wrong method, unknown URL)
  • Controllers and services stay free of error-formatting code

Standard Error Response Types

  • Basic error response: only status and message. It is the simplest form but gives little debugging information.
  • Detailed error response: adds timestamp and path to the status and message. This example uses it.
  • Validation error response: lists each invalid field with its message. Here the field messages are joined into one message string to keep the four-field structure.
  • Error response with error code: adds an application code such as STU-404, which front-end teams can use for translations and logic.
  • Problem Details (RFC 7807): the standard JSON format for HTTP API errors, supported in Spring 6 through ProblemDetail.
Scroll to Top