Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mechero/spring-boot-rest-exceptions
Custom error formatting and exception handling in Rest Controllers with Spring Boot
https://github.com/mechero/spring-boot-rest-exceptions
controller-advice error-handling exception-handler java java12 rest-api spring-boot
Last synced: 4 days ago
JSON representation
Custom error formatting and exception handling in Rest Controllers with Spring Boot
- Host: GitHub
- URL: https://github.com/mechero/spring-boot-rest-exceptions
- Owner: mechero
- Created: 2019-08-28T04:47:07.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2019-09-11T05:29:00.000Z (about 5 years ago)
- Last Synced: 2024-11-02T01:50:29.816Z (11 days ago)
- Topics: controller-advice, error-handling, exception-handler, java, java12, rest-api, spring-boot
- Language: Java
- Homepage: https://thepracticaldeveloper.com/2019/09/09/custom-error-handling-rest-controllers-spring-boot/
- Size: 329 KB
- Stars: 96
- Watchers: 2
- Forks: 29
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Handling errors in REST Controllers with Spring Boot
## Goal
This sample code shows how configure a Spring Boot application to properly handle all exceptions and create your custom error format.
Check the [blog post](https://thepracticaldeveloper.com/2019/09/09/custom-error-handling-rest-controllers-spring-boot/) on ThePracticalDeveloper site for the complete guide with instructions.
![Custom Error Handling in Spring Boot REST Controllers](img/custom-error-handling.png)
And, if you find this code useful, please give a star to the repo!
## Custom error handling
First, you'll find in this code an example of a basic approach to map exceptions to status codes in Spring Boot using a `@RestControllerAdvice` with `@ExceptionHandler` methods.
However, the most interesting part is the injection of a customized `ErrorController` implementation and the overridden `ErrorAttributes` to achieve consistent responses when dealing with errors.
For this example, I used the Google JSON Style Guide but you can use any other known error formatting or create your own one.
Check [the guide](https://thepracticaldeveloper.com/2019/09/09/custom-error-handling-rest-controllers-spring-boot/) for more details.