Skip to content

Commit 0d8ea44

Browse files
committed
Fix MyErrorWebExceptionHandler in documentation
Closes gh-38104
1 parent d9e71f7 commit 0d8ea44

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

Diff for: spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/web/reactive/webflux/errorhandling/MyErrorWebExceptionHandler.java

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,12 +18,13 @@
1818

1919
import reactor.core.publisher.Mono;
2020

21-
import org.springframework.boot.autoconfigure.web.WebProperties.Resources;
21+
import org.springframework.boot.autoconfigure.web.WebProperties;
2222
import org.springframework.boot.autoconfigure.web.reactive.error.AbstractErrorWebExceptionHandler;
2323
import org.springframework.boot.web.reactive.error.ErrorAttributes;
2424
import org.springframework.context.ApplicationContext;
2525
import org.springframework.http.HttpStatus;
2626
import org.springframework.http.MediaType;
27+
import org.springframework.http.codec.ServerCodecConfigurer;
2728
import org.springframework.stereotype.Component;
2829
import org.springframework.web.reactive.function.server.RouterFunction;
2930
import org.springframework.web.reactive.function.server.RouterFunctions;
@@ -34,9 +35,11 @@
3435
@Component
3536
public class MyErrorWebExceptionHandler extends AbstractErrorWebExceptionHandler {
3637

37-
public MyErrorWebExceptionHandler(ErrorAttributes errorAttributes, Resources resources,
38-
ApplicationContext applicationContext) {
39-
super(errorAttributes, resources, applicationContext);
38+
public MyErrorWebExceptionHandler(ErrorAttributes errorAttributes, WebProperties webProperties,
39+
ApplicationContext applicationContext, ServerCodecConfigurer serverCodecConfigurer) {
40+
super(errorAttributes, webProperties.getResources(), applicationContext);
41+
setMessageReaders(serverCodecConfigurer.getReaders());
42+
setMessageWriters(serverCodecConfigurer.getWriters());
4043
}
4144

4245
@Override

Diff for: spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/web/reactive/webflux/errorhandling/MyErrorWebExceptionHandler.kt

+11-3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import org.springframework.boot.web.reactive.error.ErrorAttributes
2222
import org.springframework.context.ApplicationContext
2323
import org.springframework.http.HttpStatus
2424
import org.springframework.http.MediaType
25+
import org.springframework.http.codec.ServerCodecConfigurer
2526
import org.springframework.stereotype.Component
2627
import org.springframework.web.reactive.function.server.RouterFunction
2728
import org.springframework.web.reactive.function.server.RouterFunctions
@@ -31,8 +32,15 @@ import reactor.core.publisher.Mono
3132

3233
@Suppress("UNUSED_PARAMETER")
3334
@Component
34-
class MyErrorWebExceptionHandler(errorAttributes: ErrorAttributes?, resources: WebProperties.Resources?,
35-
applicationContext: ApplicationContext?) : AbstractErrorWebExceptionHandler(errorAttributes, resources, applicationContext) {
35+
class MyErrorWebExceptionHandler(
36+
errorAttributes: ErrorAttributes, webProperties: WebProperties,
37+
applicationContext: ApplicationContext, serverCodecConfigurer: ServerCodecConfigurer
38+
) : AbstractErrorWebExceptionHandler(errorAttributes, webProperties.resources, applicationContext) {
39+
40+
init {
41+
setMessageReaders(serverCodecConfigurer.readers)
42+
setMessageWriters(serverCodecConfigurer.writers)
43+
}
3644

3745
override fun getRoutingFunction(errorAttributes: ErrorAttributes): RouterFunction<ServerResponse> {
3846
return RouterFunctions.route(this::acceptsXml, this::handleErrorAsXml)
@@ -42,7 +50,7 @@ class MyErrorWebExceptionHandler(errorAttributes: ErrorAttributes?, resources: W
4250
return request.headers().accept().contains(MediaType.APPLICATION_XML)
4351
}
4452

45-
fun handleErrorAsXml(request: ServerRequest?): Mono<ServerResponse> {
53+
fun handleErrorAsXml(request: ServerRequest): Mono<ServerResponse> {
4654
val builder = ServerResponse.status(HttpStatus.INTERNAL_SERVER_ERROR)
4755
// ... additional builder calls
4856
return builder.build()

0 commit comments

Comments
 (0)