Skip to content

Commit 2f13e05

Browse files
committed
Return 415 on bad request body content-type
Issue: SPR-10982
1 parent 4b38dc8 commit 2f13e05

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/AbstractMessageConverterMethodArgumentResolver.java

+12-4
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.springframework.core.GenericTypeResolver;
3434
import org.springframework.core.MethodParameter;
3535
import org.springframework.http.HttpInputMessage;
36+
import org.springframework.http.InvalidMediaTypeException;
3637
import org.springframework.http.MediaType;
3738
import org.springframework.http.converter.GenericHttpMessageConverter;
3839
import org.springframework.http.converter.HttpMessageConverter;
@@ -115,10 +116,17 @@ protected <T> Object readWithMessageConverters(NativeWebRequest webRequest,
115116
protected <T> Object readWithMessageConverters(HttpInputMessage inputMessage,
116117
MethodParameter methodParam, Type targetType) throws IOException, HttpMediaTypeNotSupportedException {
117118

118-
MediaType contentType = inputMessage.getHeaders().getContentType();
119-
if (contentType == null) {
120-
contentType = MediaType.APPLICATION_OCTET_STREAM;
121-
}
119+
MediaType contentType;
120+
try {
121+
contentType = inputMessage.getHeaders().getContentType();
122+
}
123+
catch (InvalidMediaTypeException ex) {
124+
throw new HttpMediaTypeNotSupportedException(ex.getMessage());
125+
}
126+
127+
if (contentType == null) {
128+
contentType = MediaType.APPLICATION_OCTET_STREAM;
129+
}
122130

123131
Class<?> contextClass = methodParam.getDeclaringClass();
124132
Map<TypeVariable, Type> map = GenericTypeResolver.getTypeVariableMap(contextClass);

spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestResponseBodyMethodProcessorMockTests.java

+6
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,12 @@ public void resolveArgumentNoContentType() throws Exception {
187187
}
188188
}
189189

190+
@Test(expected = HttpMediaTypeNotSupportedException.class)
191+
public void resolveArgumentInvalidContentType() throws Exception {
192+
this.servletRequest.setContentType("bad");
193+
processor.resolveArgument(paramRequestBodyString, mavContainer, webRequest, null);
194+
}
195+
190196
@Test
191197
public void resolveArgumentNotRequiredNoContent() throws Exception {
192198
servletRequest.setContent(null);

0 commit comments

Comments
 (0)