Fix @RequestBody(required=false) support
Prior to this commit, requests with an empty body and no Content-Type header set would fail with a HttpMediaTypeNotSupportedException when mapped to a Controller method argument annotated with @RequestBody(required=false). In those cases, the server implementation considers with an "application/octet-stream" content type and polls messageconverters for conversion. If no messageconverter is able to process this request, a HttpMediaTypeNotSupportedException is thrown. This change makes sure that such exceptions are not thrown if the incoming request has: * no body * no content-type header In this case, a null value is returned. Issue: SPR-13147
This commit is contained in:
@@ -158,6 +158,7 @@ public abstract class AbstractMessageConverterMethodArgumentResolver implements
|
||||
Type targetType) throws IOException, HttpMediaTypeNotSupportedException, HttpMessageNotReadableException {
|
||||
|
||||
MediaType contentType;
|
||||
boolean noContentType = false;
|
||||
try {
|
||||
contentType = inputMessage.getHeaders().getContentType();
|
||||
}
|
||||
@@ -165,6 +166,7 @@ public abstract class AbstractMessageConverterMethodArgumentResolver implements
|
||||
throw new HttpMediaTypeNotSupportedException(ex.getMessage());
|
||||
}
|
||||
if (contentType == null) {
|
||||
noContentType = true;
|
||||
contentType = MediaType.APPLICATION_OCTET_STREAM;
|
||||
}
|
||||
|
||||
@@ -220,7 +222,8 @@ public abstract class AbstractMessageConverterMethodArgumentResolver implements
|
||||
}
|
||||
|
||||
if (body == NO_VALUE) {
|
||||
if (!SUPPORTED_METHODS.contains(httpMethod)) {
|
||||
if (!SUPPORTED_METHODS.contains(httpMethod)
|
||||
|| (noContentType && inputMessage.getBody() == null)) {
|
||||
return null;
|
||||
}
|
||||
throw new HttpMediaTypeNotSupportedException(contentType, this.allSupportedMediaTypes);
|
||||
|
||||
Reference in New Issue
Block a user