DEV Community

Cover image for Handling Exceptions in Your Spring Boot REST API
Igor Venturelli
Igor Venturelli

Posted on • Originally published at igventurelli.io

Handling Exceptions in Your Spring Boot REST API

Creating a robust and user-friendly REST API with Spring Boot requires careful handling of exceptions. When errors occur, your API should return meaningful HTTP status codes instead of a generic HTTP 500 Internal Server Error. This approach enhances the clarity and usability of your API. Let's delve into how you can achieve this in a structured and effective manner.

Exceptions and HTTP Status Codes

When an exception occurs in your Spring Boot REST API, it can be mapped to an appropriate HTTP status code. For instance, if a resource is not found, a 404 Not Found status code is more informative than a generic 500 Internal Server Error. Properly handling exceptions ensures that the client receives meaningful feedback, aiding in debugging and improving the overall user experience.

Annotating Custom Exceptions

Spring Boot allows you to annotate custom exceptions, which it will automatically translate into the appropriate HTTP status codes. Here's an example:

@ResponseStatus(HttpStatus.NOT_FOUND)
public class ResourceNotFoundException extends RuntimeException {
    public ResourceNotFoundException(String message) {
        super(message);
    }
}
Enter fullscreen mode Exit fullscreen mode

In this example, the ResourceNotFoundException is annotated with @ResponseStatus(HttpStatus.NOT_FOUND). When this exception is thrown, Spring Boot will respond with a 404 Not Found status code.

Centralized Exception Handling with @ControllerAdvice

A more maintainable approach to exception handling is to use a @ControllerAdvice class. This allows you to centralize exception handling logic in one place, making your code cleaner and more modular.

Here’s how you can create an ExceptionControllerAdvice to handle exceptions globally:

@ControllerAdvice
public class ExceptionControllerAdvice {

    // this way you don't need to annotate on the exception directly
    @ExceptionHandler(ResourceNotFoundException.class)
    public ResponseEntity<String> handleResourceNotFoundException(ResourceNotFoundException ex) {
        return new ResponseEntity<>(ex.getMessage(), HttpStatus.NOT_FOUND);
    }

    // default handler, in case the exception is not catch by any other catch method
    @ExceptionHandler(Exception.class)
    public ResponseEntity<String> handleGenericException(Exception ex) {
        return new ResponseEntity<>(ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
    }
}
Enter fullscreen mode Exit fullscreen mode

In this example:

  • handleResourceNotFoundException catches ResourceNotFoundException and returns a 404 Not Found status with a custom message.
  • handleGenericException catches all other exceptions, returning a 500 Internal Server Error.

Best Practices in Exception Handling

Avoiding proper exception handling and allowing your API to return HTTP 500 for all errors is a bad practice. This approach can obscure the real issue, making it difficult for clients to understand what went wrong and how to address it. Instead, your API should be fluent and responsive, providing meaningful HTTP status codes that reflect the specific error encountered.

Conclusion

Handling exceptions in a structured manner is crucial for building robust and user-friendly APIs. By annotating custom exceptions and centralizing exception handling with @ControllerAdvice, you can ensure your API returns meaningful HTTP status codes and responses. This approach not only enhances the user experience but also aids in debugging and maintaining your API.


Let’s connect!

📧 Don't Miss a Post! Subscribe to my Newsletter!
➡️ LinkedIn
🚩 Original Post
☕ Buy me a Coffee

API Trace View

How I Cut 22.3 Seconds Off an API Call with Sentry 🕒

Struggling with slow API calls? Dan Mindru walks through how he used Sentry's new Trace View feature to shave off 22.3 seconds from an API call.

Get a practical walkthrough of how to identify bottlenecks, split tasks into multiple parallel tasks, identify slow AI model calls, and more.

Read more →

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs