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)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay