no

Stop Copying Exception Handlers Between Spring Boot Projects

If you've built more than one Spring Boot application, you've probably experienced this.

You start with a few REST endpoints and a simple @RestControllerAdvice.

@RestControllerAdvice 
public class GlobalExceptionHandler {
    @ExceptionHandler(UserNotFoundException.class) 
    public ResponseEntity < ? > handle(UserNotFoundException ex) {
      // ... 
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResponseEntity < ? > handle(MethodArgumentNotValidException ex) {
      // ... 
    }
}

Everything looks fine.

Then the application grows.

You introduce custom business exceptions. Validation errors need a different response format. Another microservice returns completely different error payloads. Feign clients need their own error decoder. Kafka consumers have another way of reporting failures.

Before long, your application no longer has one exception handling strategy—it has several.

I've seen this happen in multiple Spring Boot projects over the years.

Ironically, while the business logic was always different, the exception handling infrastructure was almost identical.

I found myself copying the same classes from project to project:

  • Global exception handlers
  • Error response models
  • Error codes
  • Validation handlers
  • Feign decoders
  • Utility classes
  • Configuration

Every new project started with the same foundation.

Eventually I stopped and asked myself:

Why am I rebuilding this every single time?

That question became nerv-exception.

What I Wanted to Build

I wasn't trying to create another framework.

Spring Boot is already an excellent framework.

Instead, I wanted a reusable library that provides a solid foundation for one specific concern:

Consistent exception handling.

The library should be:

  • Production-ready
  • Modular
  • Lightweight
  • Easy to customize
  • Easy to integrate
  • Easy to remove if needed

Introducing nerv-exception

nerv-exception is a modular Spring Boot library that standardizes exception handling across your application.

Instead of rebuilding the same infrastructure, you simply add the starter.

<dependency> 
	<groupId>com.czetsuyatech</groupId> 
	<artifactId>nerv-exception-spring-boot-starter</artifactId> 
	<version>1.0.0</version> 
</dependency>

The library provides:

  • Standardized API error responses
  • RFC 7807 Problem Details support
  • Centralized error codes
  • Global exception handling
  • Bean Validation integration
  • Spring Boot auto-configuration
  • Feign client integration
  • Event and Kafka support

Designed to Be Modular

One thing I dislike about many libraries is that they force you to adopt everything.

I wanted the opposite.

The project is divided into focused modules.

  • nerv-exception-api 
  • nerv-exception-core 
  • nerv-exception-spring-web 
  • nerv-exception-spring-boot-starter 
  • nerv-exception-spring-feign 
  • nerv-exception-event 
  • nerv-exception-spring-kafka
Applications can depend on only the modules they need.

Consistent Error Responses

One of the biggest benefits is consistency.

Instead of every controller returning a different error payload, clients receive predictable responses.

For example, a "User Not Found" error can be represented using RFC 7807 Problem Details.

  {
  "type": "https://example.com/problems/user-not-found",
  "title": "User not found",
  "status": 404,
  "detail": "User 123 was not found.",
  "instance": "/users/123"
}
  
Consistent responses make APIs easier to consume, easier to document, and easier to troubleshoot.

Real Examples Matter

Documentation is important.

But I believe runnable code is even more valuable.

That's why I also created the nerv-examples repository, which contains complete Spring Boot applications demonstrating how to integrate the library in real projects.

Rather than reading isolated snippets, you can clone the repository, run the application, and explore the implementation yourself.

Why Open Source?

Throughout my career, open source libraries have saved me thousands of hours.

This project is my opportunity to give something back.

If nerv-exception saves another developer from copying the same exception handlers into yet another Spring Boot project, then it has accomplished exactly what I hoped.

Try It Yourself

Available on Maven Central

<dependency> 
	<groupId>com.czetsuyatech</groupId> 
	<artifactId>nerv-exception-spring-boot-starter</artifactId> 
	<version>1.0.0</version> 
</dependency>

If you have ideas, questions, or suggestions, I'd love to hear them. Feedback from the community is one of the best ways to improve an open source project.

Happy coding!

Related

spring-rest 1440531714013119487

Post a Comment Default Comments

Join the Community

Week

Recent

Comments

item