Skip to content

Commit c611083

Browse files
committed
Catch IAE when parsing content type
Issue: SPR-10308
1 parent 3abe05c commit c611083

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,12 @@ else if (patternAndMethodMatches.isEmpty() && !allowedMethods.isEmpty()) {
205205
if (!consumableMediaTypes.isEmpty()) {
206206
MediaType contentType = null;
207207
if (StringUtils.hasLength(request.getContentType())) {
208-
contentType = MediaType.parseMediaType(request.getContentType());
208+
try {
209+
contentType = MediaType.parseMediaType(request.getContentType());
210+
}
211+
catch (IllegalArgumentException ex) {
212+
throw new HttpMediaTypeNotSupportedException(ex.getMessage());
213+
}
209214
}
210215
throw new HttpMediaTypeNotSupportedException(contentType, new ArrayList<MediaType>(consumableMediaTypes));
211216
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,19 @@ private void testMediaTypeNotSupported(String url) throws Exception {
180180
}
181181
}
182182

183+
@Test
184+
public void testMediaTypeNotValue() throws Exception {
185+
try {
186+
MockHttpServletRequest request = new MockHttpServletRequest("PUT", "/person/1");
187+
request.setContentType("bogus");
188+
this.handlerMapping.getHandler(request);
189+
fail("HttpMediaTypeNotSupportedException expected");
190+
}
191+
catch (HttpMediaTypeNotSupportedException ex) {
192+
assertEquals("Invalid media type \"bogus\": does not contain '/'", ex.getMessage());
193+
}
194+
}
195+
183196
@Test
184197
public void mediaTypeNotAccepted() throws Exception {
185198
testMediaTypeNotAccepted("/persons");

0 commit comments

Comments
 (0)