Skip to content
Custom error formatting and exception handling in Rest Controllers with Spring Boot
Java
Branch: master
Clone or download
Latest commit 787c8e3 Sep 11, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.mvn/wrapper Initial Commit Aug 28, 2019
img
src Added custom error code Sep 8, 2019
.gitignore Initial Commit Aug 28, 2019
README.md Updated README Sep 11, 2019
mvnw Initial Commit Aug 28, 2019
mvnw.cmd Initial Commit Aug 28, 2019
pom.xml Initial Commit Aug 28, 2019

README.md

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 on ThePracticalDeveloper site for the complete guide with instructions.

Custom Error Handling in Spring Boot REST Controllers

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 for more details.

You can’t perform that action at this time.