Handling Errors in Spring WebFlux
Last Updated :
24 Apr, 2025
The Spring Boot Community Developed the Spring Reactive Web Framework. The SpringReactive allows developers to build asynchronous, non-blocking, and event-driven web applications. When compared with the Spring MVC framework the Spring Reactive Web framework provides more functionality.
The Spring Web Flux provides different applications to developers that develop asynchronous, non-blocking web applications by using Mono and Flux, and other classes. In this article, we will learn how to handle Errors in Spring WebFlux with related examples with explanations.
Prerequisites:
- Strong Knowledge of Java Programming
- Knowledge of Spring Reactive Framework
- Developing RESTful API's
- Spring WebFlux
- Creation of Spring Reactive Project
Project Creation:
- Open Spring Tool Suite then select New Project and which Spring Stater type.
- In the project creation screen, select project categories like maven or gradle. Here, we will be using Gradle.
- Then provide the project name, package name, and other things.
- Click on next, then we will get another screen.
- In this screen, we need to select dependencies for our project.
- Now click on finish.
Project Folder Structure:

Project Dependencies:
In this project, we have used below dependencies and project category is gradle.
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-webflux'
compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'io.projectreactor:reactor-test'
}
Types of Error handling in Spring WebFlux:
In Spring WebFlux framework we handle different errors in various levels.
- Functional Level
- Global Level
Now, we will explain these two levels of Error handling in Spring WebFlux with examples.
- After creating Spring Starter project, in the project, main package create one Handler by using @Service Annotation.
- After that create one Routers for API Endpoints by using @Configuration Annotation.
- In Handler we have provided the error handling logic and in Routers class we have provided the API end points for testing.
Handling Errors at a Functional Level
In this level, we have two key operators to handle Errors in WebFlux. Those are Mono and Flux APIs. For this, we need use onErrorReturn() method to prove successful error handling in Functional Level. This onErrorReturn is returns while any error is exist in the API. Below we have provided example for both Mono and Flux with onErrorReturn() and onErrorResume()
Note: We can use onErrorReturn() to return a static default value while an error occurs. And we can use onErrorResume to return dynamic value while an error occurs.
In this example, we will explore Mono with onErrorReturn() and Flux with onErrorResume(). Here The Mono and Flux always return a publisher which is types of server Response.
Example:
Here, we have created one handler class for creating our logic in this Java class. By using @Service Annotation It possible below we have provided the entire code for handling errors in functional level with Mono and Flux with onErrorReturn() and onErrorResume().
ServiceHandler.java:
Java
package com.webflux.app;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;
@Service
public class ServiceHandler {
// api for mono with on error return method
public Mono<ServerResponse> errorHandlingAtFunctionalLevelWithMono(ServerRequest request){
return sayHello(request)
.onErrorReturn("This is Error Handling At Functional Level with Mono with onErrorReturn")
.flatMap(response -> ServerResponse.ok()
.contentType(MediaType.TEXT_PLAIN)
.bodyValue(response));
}
// api for flux with on error resume method
public Mono<ServerResponse> errorHandlingAtFunctionalLevelWithFlux(ServerRequest request){
return sayHello(request)
.onErrorResume(error -> {
System.err.println("Error occurred: " + error.getMessage());
return Mono.just("This is Error Handling At Functional Level With Flux and onErrorResume()");
})
.flatMap(response -> ServerResponse.ok()
.contentType(MediaType.TEXT_PLAIN)
.bodyValue(response));
}
// used for creating a runtime error
public Mono<String> sayHello(ServerRequest request) {
return Mono.error(new RuntimeException("Exception is Raised"));
}
}