Return 415 on bad request body content-type

Issue: SPR-10982
This commit is contained in:
Rossen Stoyanchev
2013-11-05 15:30:22 -05:00
parent 4b38dc8972
commit 2f13e05b4f
2 changed files with 18 additions and 4 deletions

View File

@@ -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);

View File

@@ -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);