Handling Errors at a Global Level
This is another case for handling errors in Spring WebFlux. In this category we handle errors at Global level for we need use DefaultErrorAttributes class which is availble in built-in this class is used for handling errors at global level. To do this we have two steps are there:
- Customize the Global Error Response Attributes for returning errors attributes in the form bad request.
- Implement the Global Error Handler for handling global errors.
When Exception is occurs, our handler throws will be automatically transform to HTTP Status. Here we override the getErrorAttributes(). Below we have provided that code for reference.
Here, we have created two different classes one for handling global errors another one is used for creating error attributes below provide them. And The Handler is Component type and the Error attribute class is normal class.
Error Handler:
Java
package com.webflux.app; import java.util.Map; import org.springframework.boot.autoconfigure.web.reactive.error.AbstractErrorWebExceptionHandler; import org.springframework.boot.web.codec.CodecCustomizer; import org.springframework.boot.web.error.ErrorAttributeOptions; import org.springframework.boot.web.reactive.error.ErrorAttributes; import org.springframework.context.ApplicationContext; import org.springframework.core.annotation.Order; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.codec.ServerCodecConfigurer; import org.springframework.stereotype.Component; import org.springframework.web.reactive.function.BodyInserters; import org.springframework.web.reactive.function.server.RequestPredicates; import org.springframework.web.reactive.function.server.RouterFunction; import org.springframework.web.reactive.function.server.RouterFunctions; import org.springframework.web.reactive.function.server.ServerRequest; import org.springframework.web.reactive.function.server.ServerResponse; import reactor.core.publisher.Mono; @Component @Order (- 2 ) public class GlobalErrorWebExceptionHandler extends AbstractErrorWebExceptionHandler { public GlobalErrorWebExceptionHandler(ErrorAttributes errorAttributes, ApplicationContext applicationContext, CodecCustomizer serverCodecConfigurer) { super (errorAttributes, new CodecCustomizer[]{serverCodecConfigurer}); } @Override protected RouterFunction<ServerResponse> getRoutingFunction(ErrorAttributes errorAttributes) { return RouterFunctions.route(RequestPredicates.all(), this ::renderErrorResponse); } private Mono<ServerResponse> renderErrorResponse(ServerRequest request) { Map<String, Object> errorPropertiesMap = getErrorAttributes(request, ErrorAttributeOptions.defaults()); return ServerResponse.status(HttpStatus.BAD_REQUEST) .contentType(MediaType.APPLICATION_JSON) .body(BodyInserters.fromValue(errorPropertiesMap)); } } |
ErrorAttributes:
Java
package com.webflux.app; import java.util.Map; import org.springframework.boot.web.error.ErrorAttributeOptions; import org.springframework.http.HttpStatus; import org.springframework.web.reactive.function.server.ServerRequest; public class GlobalErrorAttributes extends org.springframework.boot.web.reactive.error.DefaultErrorAttributes{ @Override public Map<String, Object> getErrorAttributes(ServerRequest request, ErrorAttributeOptions options) { Map<String, Object> map = super .getErrorAttributes( request, options); map.put( "status" , HttpStatus.BAD_REQUEST); map.put( "message" , "This is Global error" ); return map; } } |
After successfully run this project, we will get below output.
Output:
status : 400
message : This is Global error
Handling Errors in Spring WebFlux
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'
}
Contact Us