eBook – Guide Spring Cloud – NPI EA (cat=Spring Cloud)
announcement - icon

Let's get started with a Microservice Architecture with Spring Cloud:

>> Join Pro and download the eBook

eBook – Mockito – NPI EA (tag = Mockito)
announcement - icon

Mocking is an essential part of unit testing, and the Mockito library makes it easy to write clean and intuitive unit tests for your Java code.

Get started with mocking and improve your application tests using our Mockito guide:

Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Reactive – NPI EA (cat=Reactive)
announcement - icon

Spring 5 added support for reactive programming with the Spring WebFlux module, which has been improved upon ever since. Get started with the Reactor project basics and reactive programming in Spring Boot:

>> Join Pro and download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Jackson – NPI EA (cat=Jackson)
announcement - icon

Do JSON right with Jackson

Download the E-book

eBook – HTTP Client – NPI EA (cat=Http Client-Side)
announcement - icon

Get the most out of the Apache HTTP Client

Download the E-book

eBook – Maven – NPI EA (cat = Maven)
announcement - icon

Get Started with Apache Maven:

Download the E-book

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

eBook – RwS – NPI EA (cat=Spring MVC)
announcement - icon

Building a REST API with Spring?

Download the E-book

Course – LS – NPI EA (cat=Jackson)
announcement - icon

Get started with Spring and Spring Boot, through the Learn Spring course:

>> LEARN SPRING
Course – RWSB – NPI EA (cat=REST)
announcement - icon

Explore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:

>> The New “REST With Spring Boot”

Course – LSS – NPI EA (cat=Spring Security)
announcement - icon

Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.

I built the security material as two full courses - Core and OAuth, to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project.

You can explore the course here:

>> Learn Spring Security

Course – LSD – NPI EA (tag=Spring Data JPA)
announcement - icon

Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot.

Get started with Spring Data JPA through the guided reference course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – LJB – NPI EA (cat = Core Java)
announcement - icon

Code your way through and build up a solid, practical foundation of Java:

>> Learn Java Basics

eBook – Guide Spring Cloud – NPI (cat=Cloud/Spring Cloud)
announcement - icon

Let's get started with a Microservice Architecture with Spring Cloud:

>> Join Pro and download the eBook

1. Overview

In this tutorial, we’ll explore the nuances of implementing a global exception-handling strategy within Spring Cloud Gateway, delving into its technicalities and best practices.

In modern software development, especially in microservices, efficient management of APIs is crucial. This is where Spring Cloud Gateway plays an important role as a key component of the Spring ecosystem. It acts like a gatekeeper, directing traffic and requests to the appropriate microservices and providing cross-cutting concerns, such as security, monitoring/metrics, and resiliency.

However, in such a complex environment, the certainty of exceptions due to network failures, service downtime, or application bugs demands a robust exception-handling mechanism. The global exception handling in Spring Cloud Gateway ensures a consistent approach for error handling across all services and enhances the entire system’s resilience and reliability.

2. The Need for Global Exception Handling

Spring Cloud Gateway is a project part of the Spring ecosystem, designed to serve as an API gateway in microservices architectures, and its main role is to route requests to the appropriate microservices based on pre-established rules. The Gateway provides functionalities like security (authentication and authorization), monitoring, and resilience (circuit breakers). By handling requests and directing them to appropriate backend services, it effectively manages cross-cutting concerns like security and traffic management.

In distributed systems like microservices, exceptions may arise from multiple sources, such as network issues, service unavailability, downstream service errors, and application-level bugs, which are common culprits. In such environments, handling exceptions in a localized manner (i.e., within each service) can lead to fragmented and inconsistent error handling. This inconsistency can make debugging cumbersome and degrade the user’s experience:

API Gateway Error Handling Diagram

Global exception handling addresses this challenge by providing a centralized exception management mechanism that ensures that all exceptions, regardless of their source, are processed consistently, providing standardized error responses.

This consistency is critical for system resilience, simplifying error tracking and analysis. It also enhances the user’s experience by providing a precise and consistent error format, helping users understand what went wrong.

3. Implementing Global Exception Handling in Spring Cloud Gateway

Implementing global exception handling in Spring Cloud Gateway involves several critical steps, each ensuring a robust and efficient error management system.

3.1. Creating a Custom Global Exception Handler

A global exception handler is essential for catching and handling exceptions that occur anywhere within the Gateway. To do that, we need to extend AbstractErrorWebExceptionHandler and add it to the Spring context. By doing so, we create a centralized handler that intercepts all exceptions.

@Component
public class CustomGlobalExceptionHandler extends AbstractErrorWebExceptionHandler {
    // Constructor and methods
}

This class should be designed to handle various types of exceptions, from general exceptions like NullPointerException to more specific ones like HttpClientErrorException. The goal is to cover a broad spectrum of possible errors. The main method of such a class is shown below.

@Override
protected RouterFunction<ServerResponse> getRoutingFunction(ErrorAttributes errorAttributes) {
    return RouterFunctions.route(RequestPredicates.all(), this::renderErrorResponse);
}

// other methods

In this method, we can apply a handler function to the error based on a predicate evaluated using the current request and deal with it properly. It’s important to notice that the global exception handler only deals with exceptions thrown within the gateway context. That means response codes like  5xx or 4xx are not included in the context of the global exception handler, and those should be handled using route or global filters.

The AbstractErrorWebExceptionHandler offers many methods that help us deal with the exceptions thrown during the request handling.

private Mono<ServerResponse> renderErrorResponse(ServerRequest request) {
    ErrorAttributeOptions options = ErrorAttributeOptions.of(ErrorAttributeOptions.Include.MESSAGE);
    Map<String, Object> errorPropertiesMap = getErrorAttributes(request, options);
    Throwable throwable = getError(request);
    HttpStatusCode httpStatus = determineHttpStatus(throwable);

    errorPropertiesMap.put("status", httpStatus.value());
    errorPropertiesMap.remove("error");

    return ServerResponse.status(httpStatus)
      .contentType(MediaType.APPLICATION_JSON_UTF8)
      .body(BodyInserters.fromObject(errorPropertiesMap));
}

private HttpStatusCode determineHttpStatus(Throwable throwable) {
    if (throwable instanceof ResponseStatusException) {
        return ((ResponseStatusException) throwable).getStatusCode();
    } else if (throwable instanceof CustomRequestAuthException) {
        return HttpStatus.UNAUTHORIZED;
    } else if (throwable instanceof RateLimitRequestException) {
        return HttpStatus.TOO_MANY_REQUESTS;
    } else {
        return HttpStatus.INTERNAL_SERVER_ERROR;
    }
}

Looking at the code above, two methods provided by the Spring team are relevant. These are getErrorAttributes() and getError(), and such methods provide context as well as error information, which are important to handle the exceptions properly.

Finally, these methods collect the data provided by Spring context, hide some details, and adjust the status code and response based on the exception type. The CustomRequestAuthException and RateLimitRequestException are custom exceptions that will be further explored soon.

3.2. Configuring the GatewayFilter

The gateway filters are components that intercept all incoming requests and outgoing responses:

Spring cloud gateway diagram

By implementing GatewayFilter or GlobalFilter and adding it to the Spring context, we ensure that requests are handled uniformly and properly

public class MyCustomFilter implements GatewayFilter {
    // Implementation details
}

This filter can be used to log incoming requests, which is helpful for debugging. In the event of an exception, the filter should redirect the flow to the GlobalExceptionHandler. The difference between them is that GlobalFilter targets all upcoming requests, while GatewayFilter targets particular routes defined in the RouteLocator.

Next, let’s have a look at two samples of filter implementations:

public class MyCustomFilter implements GatewayFilter {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        if (isAuthRoute(exchange) && !isAuthorization(exchange)) {
            throw new CustomRequestAuthException("Not authorized");
        }

        return chain.filter(exchange);
    }

    private static boolean isAuthorization(ServerWebExchange exchange) {
        return exchange.getRequest().getHeaders().containsKey("Authorization");
    }

    private static boolean isAuthRoute(ServerWebExchange exchange) {
        return exchange.getRequest().getURI().getPath().equals("/test/custom_auth");
    }
}

The MyCustomFilter in our sample simulates a gateway validation. The idea was to fail and avoid the request if no authorization header is present. If that were the case, the exception would be thrown, handing the error to the global exception handler.

@Component
class MyGlobalFilter implements GlobalFilter {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        if (hasReachedRateLimit(exchange)) {
            throw new RateLimitRequestException("Too many requests");
        }

        return chain.filter(exchange);
    }

    private boolean hasReachedRateLimit(ServerWebExchange exchange) {
        // Simulates the rate limit being reached
        return exchange.getRequest().getURI().getPath().equals("/test/custom_rate_limit") && 
          (!exchange.getRequest().getHeaders().containsKey("X-RateLimit-Remaining") || 
            Integer.parseInt(exchange.getRequest().getHeaders().getFirst("X-RateLimit-Remaining")) <= 0);
    }
}

Lastly, in MyGlobalFilter, the filter checks all requests but only fails for a particular route. It simulates the validation of a rate limit using headers. As it is a GlobalFilter, we need to add it to the Spring context.

Once again, once the exception happens, the global exception handler takes care of response management.

3.3. Uniform Exception Handling

Consistency in exception handling is vital. This involves setting up a standard error response format, including the HTTP status code, an error message (response body), and any additional information that might be helpful for debugging or user comprehension.

private Mono<ServerResponse> renderErrorResponse(ServerRequest request) {
    // Define our error response structure here
}

Using this approach, we can adapt the response based on the exception type. For example, a 500 Internal Server problem indicates a server-side exception, a 400 Bad Request indicates a client-side problem, and so on. As we saw in our sample, Spring context already provides some data, but the response can be customized.

4. Advanced Considerations

Advanced considerations include implementing enhanced logging for all exceptions. This can involve integrating external monitoring and logging tools like Splunk, ELK Stack, etc. Additionally, categorizing exceptions and customizing error messages based on these categories can significantly aid in troubleshooting and improving user communication.

Testing is crucial to ensure the effectiveness of your global exception handlers. This involves writing unit and integration tests to simulate various exception scenarios. Tools like JUnit and Mockito are instrumental in this process, allowing you to mock services and test how your exception handler responds to different exceptions.

5. Conclusion

Best practices in implementing global exception handling include keeping the error-handling logic simple and comprehensive. It’s important to log every exception for future analysis and periodically update the handling logic as new exceptions are identified. Regularly reviewing the exception-handling mechanisms also helps keep up with the evolving microservices architecture.

Implementing global exception handling in Spring Cloud Gateway is crucial to developing robust microservices architecture. It ensures a consistent error-handling strategy across all services and significantly improves the system’s resilience and reliability. Developers can build a more user-friendly and maintainable system by following this article’s implementation strategies and best practices.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Baeldung Pro – NPI EA (cat = Baeldung)
announcement - icon

Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:

>> Explore a clean Baeldung

Once the early-adopter seats are all used, the price will go up and stay at $33/year.

eBook – HTTP Client – NPI EA (cat=HTTP Client-Side)
announcement - icon

The Apache HTTP Client is a very robust library, suitable for both simple and advanced use cases when testing HTTP endpoints. Check out our guide covering basic request and response handling, as well as security, cookies, timeouts, and more:

>> Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

Course – LS – NPI EA (cat=REST)

announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

eBook Jackson – NPI EA – 3 (cat = Jackson)
eBook – eBook Guide Spring Cloud – NPI (cat=Cloud/Spring Cloud)
2 Comments
Oldest
Newest
Inline Feedbacks
View all comments