Skip to content

alimate/Resource-Based-Error-Codes

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

REST API Error Handling with Spring Boot

This is a sample project to show how to implement a robust error handling mechanism for REST APIs written with Spring Boot. For more details on goals and motivations of this repository you can check this post out.

HTTP Error Response Schema

The generated error response would have the following schema:

{
  "status_code": // integer,
  "reason_phrase": // string,
  "errors": [
    {
      "error_code": // string,
      "message": // string
    }, ...
  ]
}

How it Works?

On Linux/Mac machines, use the following command:

./gradlew bootRun

and in Windows machines:

gradlew.bat bootRun

Then send a POST request with an empty JSON payload, i.e. {}, to /geeks endpoint. You should get the following as the response:

{
  "status_code": 400,
  "reason_phrase": "Bad Request",
  "errors": [
    {
      "error_code": "geeks-3",
      "message": "last_name is required"
    },
    {
      "error_code": "geeks-2",
      "message": "first_name is required"
    }
  ]
}

You could also set the Accept-Language to fa-IR to get the localized error messages. Also, if you send arbitrary first_name and last_name in the request body, you'll get the following response:

{
  "status_code": 400,
  "reason_phrase": "Bad Request",
  "errors": [
    {
      "error_code": "geeks-1",
      "message": "The geek already exists"
    }
  ]
}

Releases

No releases published

Packages

No packages published

Languages