Açıklaması şöyle
The custom exception system provided in SpringMVC doesn’t work in Spring-WebFlux for the simple reason that the underlying runtime containers are not the same. WebExceptionHandler is the top-level interface to the exception handler of Spring-WebFlux,
Örnek
Açıklaması şöyle
This class extends the AbstractErrorWebExceptionHandler class provided by the spring and custom implements the handling of the Global Exception. @Order(-2) is used to give a priority boost to the component that the Spring Boot class DefaultErrorWebExceptionHandler which is of Order(-1) priority.
Şöyle yaparız
@Component@Order(-2)public class GlobalErrorWebExceptionHandler extends AbstractErrorWebExceptionHandler {public GlobalErrorWebExceptionHandler(GlobalErrorAttributes globalErrorAttributes, ApplicationContext applicationContext,ServerCodecConfigurer serverCodecConfigurer) {super(globalErrorAttributes, new WebProperties.Resources(), applicationContext);super.setMessageWriters(serverCodecConfigurer.getWriters());super.setMessageReaders(serverCodecConfigurer.getReaders());}@Overrideprotected 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());int statusCode = Integer.parseInt(errorPropertiesMap.get(ErrorAttributesKey.CODE.getKey()) .toString());return ServerResponse.status(HttpStatus.valueOf(statusCode)).contentType(MediaType.APPLICATION_JSON).body(BodyInserters.fromValue(errorPropertiesMap));}}
Hiç yorum yok:
Yorum Gönder