Lenient treatment of malformed Accept header for @ExceptionHandler
Closes gh-24539
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2020 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.
|
||||
@@ -213,7 +213,21 @@ public abstract class AbstractMessageConverterMethodProcessor extends AbstractMe
|
||||
}
|
||||
else {
|
||||
HttpServletRequest request = inputMessage.getServletRequest();
|
||||
List<MediaType> acceptableTypes = getAcceptableMediaTypes(request);
|
||||
List<MediaType> acceptableTypes;
|
||||
try {
|
||||
acceptableTypes = getAcceptableMediaTypes(request);
|
||||
}
|
||||
catch (HttpMediaTypeNotAcceptableException ex) {
|
||||
int series = outputMessage.getServletResponse().getStatus() / 100;
|
||||
if (body == null || series == 4 || series == 5) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Ignoring error response content (if any). " + ex);
|
||||
}
|
||||
logger.debug(ex.getMessage());
|
||||
return;
|
||||
}
|
||||
throw ex;
|
||||
}
|
||||
List<MediaType> producibleTypes = getProducibleMediaTypes(request, valueType, targetType);
|
||||
|
||||
if (body != null && producibleTypes.isEmpty()) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2020 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.
|
||||
@@ -209,7 +209,6 @@ public class HttpEntityMethodProcessorTests {
|
||||
|
||||
@Test // SPR-13423
|
||||
public void handleReturnValueWithETagAndETagFilter() throws Exception {
|
||||
|
||||
String eTagValue = "\"deadb33f8badf00d\"";
|
||||
String content = "body";
|
||||
|
||||
@@ -242,6 +241,25 @@ public class HttpEntityMethodProcessorTests {
|
||||
assertThat(this.servletResponse.getContentAsString()).isEqualTo(content);
|
||||
}
|
||||
|
||||
@Test // gh-24539
|
||||
public void handleReturnValueWithMalformedAcceptHeader() throws Exception {
|
||||
webRequest.getNativeRequest(MockHttpServletRequest.class).addHeader("Accept", "null");
|
||||
|
||||
List<HttpMessageConverter<?>>converters = new ArrayList<>();
|
||||
converters.add(new ByteArrayHttpMessageConverter());
|
||||
converters.add(new StringHttpMessageConverter());
|
||||
|
||||
Method method = getClass().getDeclaredMethod("handle");
|
||||
MethodParameter returnType = new MethodParameter(method, -1);
|
||||
ResponseEntity<String> returnValue = ResponseEntity.badRequest().body("Foo");
|
||||
|
||||
HttpEntityMethodProcessor processor = new HttpEntityMethodProcessor(converters);
|
||||
processor.handleReturnValue(returnValue, returnType, mavContainer, webRequest);
|
||||
|
||||
assertThat(servletResponse.getStatus()).isEqualTo(400);
|
||||
assertThat(servletResponse.getHeader("Content-Type")).isNull();
|
||||
assertThat(servletResponse.getContentAsString()).isEmpty();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private void handle(HttpEntity<List<SimpleBean>> arg1, HttpEntity<SimpleBean> arg2) {
|
||||
|
||||
Reference in New Issue
Block a user