From 0d8ea44f5baf7f6315379191e226ca0385ec1dda Mon Sep 17 00:00:00 2001 From: Moritz Halbritter Date: Mon, 30 Oct 2023 10:07:49 +0100 Subject: [PATCH] Fix MyErrorWebExceptionHandler in documentation Closes gh-38104 --- .../errorhandling/MyErrorWebExceptionHandler.java | 13 ++++++++----- .../errorhandling/MyErrorWebExceptionHandler.kt | 14 +++++++++++--- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/web/reactive/webflux/errorhandling/MyErrorWebExceptionHandler.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/web/reactive/webflux/errorhandling/MyErrorWebExceptionHandler.java index 586ca2cff56..b2cb34518b8 100644 --- a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/web/reactive/webflux/errorhandling/MyErrorWebExceptionHandler.java +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/web/reactive/webflux/errorhandling/MyErrorWebExceptionHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2021 the original author or authors. + * Copyright 2012-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,12 +18,13 @@ package org.springframework.boot.docs.web.reactive.webflux.errorhandling; import reactor.core.publisher.Mono; -import org.springframework.boot.autoconfigure.web.WebProperties.Resources; +import org.springframework.boot.autoconfigure.web.WebProperties; import org.springframework.boot.autoconfigure.web.reactive.error.AbstractErrorWebExceptionHandler; 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.server.RouterFunction; import org.springframework.web.reactive.function.server.RouterFunctions; @@ -34,9 +35,11 @@ import org.springframework.web.reactive.function.server.ServerResponse.BodyBuild @Component public class MyErrorWebExceptionHandler extends AbstractErrorWebExceptionHandler { - public MyErrorWebExceptionHandler(ErrorAttributes errorAttributes, Resources resources, - ApplicationContext applicationContext) { - super(errorAttributes, resources, applicationContext); + public MyErrorWebExceptionHandler(ErrorAttributes errorAttributes, WebProperties webProperties, + ApplicationContext applicationContext, ServerCodecConfigurer serverCodecConfigurer) { + super(errorAttributes, webProperties.getResources(), applicationContext); + setMessageReaders(serverCodecConfigurer.getReaders()); + setMessageWriters(serverCodecConfigurer.getWriters()); } @Override diff --git a/spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/web/reactive/webflux/errorhandling/MyErrorWebExceptionHandler.kt b/spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/web/reactive/webflux/errorhandling/MyErrorWebExceptionHandler.kt index d252b81555e..3ed09fd1e0b 100644 --- a/spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/web/reactive/webflux/errorhandling/MyErrorWebExceptionHandler.kt +++ b/spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/web/reactive/webflux/errorhandling/MyErrorWebExceptionHandler.kt @@ -22,6 +22,7 @@ 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.server.RouterFunction import org.springframework.web.reactive.function.server.RouterFunctions @@ -31,8 +32,15 @@ import reactor.core.publisher.Mono @Suppress("UNUSED_PARAMETER") @Component -class MyErrorWebExceptionHandler(errorAttributes: ErrorAttributes?, resources: WebProperties.Resources?, - applicationContext: ApplicationContext?) : AbstractErrorWebExceptionHandler(errorAttributes, resources, applicationContext) { +class MyErrorWebExceptionHandler( + errorAttributes: ErrorAttributes, webProperties: WebProperties, + applicationContext: ApplicationContext, serverCodecConfigurer: ServerCodecConfigurer +) : AbstractErrorWebExceptionHandler(errorAttributes, webProperties.resources, applicationContext) { + + init { + setMessageReaders(serverCodecConfigurer.readers) + setMessageWriters(serverCodecConfigurer.writers) + } override fun getRoutingFunction(errorAttributes: ErrorAttributes): RouterFunction { return RouterFunctions.route(this::acceptsXml, this::handleErrorAsXml) @@ -42,7 +50,7 @@ class MyErrorWebExceptionHandler(errorAttributes: ErrorAttributes?, resources: W return request.headers().accept().contains(MediaType.APPLICATION_XML) } - fun handleErrorAsXml(request: ServerRequest?): Mono { + fun handleErrorAsXml(request: ServerRequest): Mono { val builder = ServerResponse.status(HttpStatus.INTERNAL_SERVER_ERROR) // ... additional builder calls return builder.build()