Skip to content

Commit e47f7ef

Browse files
committed
Remove response content-type before error handling
Prior to this commit, the negotiated content-type during the request mapping phase would be kept as the response content-type header; this information is used when rendering the error response and prevents a new round of content negotiation to choose the media type that fits best. This commit removes the response content type information at the beginning of the error handling phase. Fixes gh-22452
1 parent 7c65b57 commit e47f7ef

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/RequestMappingHandlerAdapter.java

+4
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.springframework.context.ApplicationContextAware;
3030
import org.springframework.context.ConfigurableApplicationContext;
3131
import org.springframework.core.ReactiveAdapterRegistry;
32+
import org.springframework.http.HttpHeaders;
3233
import org.springframework.http.codec.HttpMessageReader;
3334
import org.springframework.http.codec.ServerCodecConfigurer;
3435
import org.springframework.lang.Nullable;
@@ -209,6 +210,9 @@ private Mono<HandlerResult> handleException(Throwable exception, HandlerMethod h
209210

210211
// Success and error responses may use different content types
211212
exchange.getAttributes().remove(HandlerMapping.PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE);
213+
if (!exchange.getResponse().isCommitted()) {
214+
exchange.getResponse().getHeaders().remove(HttpHeaders.CONTENT_TYPE);
215+
}
212216

213217
InvocableHandlerMethod invocable = this.methodResolver.getExceptionHandlerMethod(exception, handlerMethod);
214218
if (invocable != null) {

spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/RequestMappingExceptionHandlingIntegrationTests.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public void exceptionAfterSeveralItems() {
9393
public void exceptionFromMethodWithProducesCondition() throws Exception {
9494
try {
9595
HttpHeaders headers = new HttpHeaders();
96-
headers.add("Accept", "text/csv, application/problem+json");
96+
headers.add("Accept", "text/plain, application/problem+json");
9797
performGet("/SPR-16318", headers, String.class).getBody();
9898
fail();
9999
}
@@ -152,9 +152,9 @@ public Flux<String> errors() {
152152
});
153153
}
154154

155-
@GetMapping(path = "/SPR-16318", produces = "text/csv")
156-
public String handleCsv() throws Exception {
157-
throw new Spr16318Exception();
155+
@GetMapping(path = "/SPR-16318", produces = "text/plain")
156+
public Mono<String> handleTextPlain() throws Exception {
157+
return Mono.error(new Spr16318Exception());
158158
}
159159

160160
@ExceptionHandler

0 commit comments

Comments
 (0)