package com.webflexexample.webflexExample.Exception; import lombok.AllArgsConstructor; import org.springframework.boot.autoconfigure.web.WebProperties; import org.springframework.boot.autoconfigure.web.reactive.error.AbstractErrorWebExceptionHandler; import org.springframework.boot.web.error.ErrorAttributeOptions; import org.springframework.boot.web.reactive.error.ErrorAttributes; import org.springframework.context.ApplicationContext; 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.*; import reactor.core.publisher.Mono; import java.util.Map; @Component public class CustomExceptionHandler extends AbstractErrorWebExceptionHandler { public CustomExceptionHandler(ErrorAttributes errorAttributes, WebProperties.Resources resources, ApplicationContext applicationContext, ServerCodecConfigurer configurer) { super(errorAttributes, resources, applicationContext); this.setMessageReaders(configurer.getReaders()); this.setMessageWriters(configurer.getWriters()); } @Override protected RouterFunction getRoutingFunction(ErrorAttributes errorAttributes) { return RouterFunctions.route(RequestPredicates.all(),this::renderException); } private Mono renderException(ServerRequest request) { Map error = this.getErrorAttributes(request, ErrorAttributeOptions.defaults()); error.remove("status"); error.remove("requestId"); return ServerResponse.status(HttpStatus.BAD_REQUEST).contentType(MediaType.APPLICATION_JSON) .body(BodyInserters.fromValue(error)); } }