Browse Source

Return 415 on bad request body content-type

Issue: SPR-10982
pull/405/head
Rossen Stoyanchev 12 years ago
parent
commit
2f13e05b4f
  1. 16
      spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/AbstractMessageConverterMethodArgumentResolver.java
  2. 6
      spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestResponseBodyMethodProcessorMockTests.java

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

@ -33,6 +33,7 @@ import org.apache.commons.logging.LogFactory; @@ -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 @@ -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);

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

@ -187,6 +187,12 @@ public class RequestResponseBodyMethodProcessorMockTests { @@ -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);

Loading…
Cancel
Save