Lenient treatment of malformed Accept header for @ExceptionHandler

Closes gh-24539
This commit is contained in:
Rossen Stoyanchev
2021-02-24 20:44:50 +00:00
parent b5147a034c
commit aa73f6733e
5 changed files with 96 additions and 21 deletions

View File

@@ -29,6 +29,7 @@ import org.springframework.core.ReactiveAdapter;
import org.springframework.core.ReactiveAdapterRegistry;
import org.springframework.core.ResolvableType;
import org.springframework.core.codec.Hints;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.codec.HttpMessageWriter;
import org.springframework.http.converter.HttpMessageNotWritableException;
@@ -144,7 +145,20 @@ public abstract class AbstractMessageWriterResultHandler extends HandlerResultHa
return Mono.from((Publisher<Void>) publisher);
}
MediaType bestMediaType = selectMediaType(exchange, () -> getMediaTypesFor(elementType));
MediaType bestMediaType;
try {
bestMediaType = selectMediaType(exchange, () -> getMediaTypesFor(elementType));
}
catch (NotAcceptableStatusException ex) {
HttpStatus statusCode = exchange.getResponse().getStatusCode();
if (statusCode != null && statusCode.isError()) {
if (logger.isDebugEnabled()) {
logger.debug("Ignoring error response content (if any). " + ex.getReason());
}
return Mono.empty();
}
throw ex;
}
if (bestMediaType != null) {
String logPrefix = exchange.getLogPrefix();
if (logger.isDebugEnabled()) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2021 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.
@@ -320,7 +320,20 @@ public class ViewResolutionResultHandler extends HandlerResultHandlerSupport imp
}
}
List<MediaType> mediaTypes = getMediaTypes(views);
MediaType bestMediaType = selectMediaType(exchange, () -> mediaTypes);
MediaType bestMediaType;
try {
bestMediaType = selectMediaType(exchange, () -> mediaTypes);
}
catch (NotAcceptableStatusException ex) {
HttpStatus statusCode = exchange.getResponse().getStatusCode();
if (statusCode != null && statusCode.isError()) {
if (logger.isDebugEnabled()) {
logger.debug("Ignoring error response content (if any). " + ex.getReason());
}
return Mono.empty();
}
throw ex;
}
if (bestMediaType != null) {
for (View view : views) {
for (MediaType mediaType : view.getSupportedMediaTypes()) {