Return 415 on bad request body content-type
Issue: SPR-10982
This commit is contained in:
@@ -33,6 +33,7 @@ import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.core.GenericTypeResolver;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.http.HttpInputMessage;
|
||||
import org.springframework.http.InvalidMediaTypeException;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.converter.GenericHttpMessageConverter;
|
||||
import org.springframework.http.converter.HttpMessageConverter;
|
||||
@@ -115,10 +116,17 @@ public abstract class AbstractMessageConverterMethodArgumentResolver implements
|
||||
protected <T> Object readWithMessageConverters(HttpInputMessage inputMessage,
|
||||
MethodParameter methodParam, Type targetType) throws IOException, HttpMediaTypeNotSupportedException {
|
||||
|
||||
MediaType contentType = inputMessage.getHeaders().getContentType();
|
||||
if (contentType == null) {
|
||||
contentType = MediaType.APPLICATION_OCTET_STREAM;
|
||||
}
|
||||
MediaType contentType;
|
||||
try {
|
||||
contentType = inputMessage.getHeaders().getContentType();
|
||||
}
|
||||
catch (InvalidMediaTypeException ex) {
|
||||
throw new HttpMediaTypeNotSupportedException(ex.getMessage());
|
||||
}
|
||||
|
||||
if (contentType == null) {
|
||||
contentType = MediaType.APPLICATION_OCTET_STREAM;
|
||||
}
|
||||
|
||||
Class<?> contextClass = methodParam.getDeclaringClass();
|
||||
Map<TypeVariable, Type> map = GenericTypeResolver.getTypeVariableMap(contextClass);
|
||||
|
||||
@@ -187,6 +187,12 @@ public class RequestResponseBodyMethodProcessorMockTests {
|
||||
}
|
||||
}
|
||||
|
||||
@Test(expected = HttpMediaTypeNotSupportedException.class)
|
||||
public void resolveArgumentInvalidContentType() throws Exception {
|
||||
this.servletRequest.setContentType("bad");
|
||||
processor.resolveArgument(paramRequestBodyString, mavContainer, webRequest, null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveArgumentNotRequiredNoContent() throws Exception {
|
||||
servletRequest.setContent(null);
|
||||
|
||||
Reference in New Issue
Block a user