diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java index ae195b02d4..c521913223 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java @@ -485,7 +485,10 @@ public class ResourceHttpRequestHandler extends WebContentGenerator if (mediaType == null) { ServletWebRequest webRequest = new ServletWebRequest(request); try { - getContentNegotiationManager().resolveMediaTypes(webRequest); + List mediaTypes = getContentNegotiationManager().resolveMediaTypes(webRequest); + if(!mediaTypes.isEmpty()) { + mediaType = mediaTypes.get(0); + } } catch (HttpMediaTypeNotAcceptableException ex) { // Ignore diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandlerTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandlerTests.java index 3083810f32..feaf098322 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandlerTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandlerTests.java @@ -54,6 +54,7 @@ import org.springframework.web.servlet.HandlerMapping; * @author Keith Donald * @author Jeremy Grelle * @author Rossen Stoyanchev + * @author Brian Clozel */ public class ResourceHttpRequestHandlerTests { @@ -244,6 +245,27 @@ public class ResourceHttpRequestHandlerTests { assertEquals("h1 { color:red; }", this.response.getContentAsString()); } + @Test // SPR-13658 + public void getResourceWithRegisteredMediaTypeDefaultStrategy() throws Exception { + ContentNegotiationManagerFactoryBean factory = new ContentNegotiationManagerFactoryBean(); + factory.setFavorPathExtension(false); + factory.setDefaultContentType(new MediaType("foo", "bar")); + factory.afterPropertiesSet(); + ContentNegotiationManager manager = factory.getObject(); + + List paths = Collections.singletonList(new ClassPathResource("test/", getClass())); + this.handler = new ResourceHttpRequestHandler(); + this.handler.setLocations(paths); + this.handler.setContentNegotiationManager(manager); + this.handler.afterPropertiesSet(); + + this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.css"); + this.handler.handleRequest(this.request, this.response); + + assertEquals("foo/bar", this.response.getContentType()); + assertEquals("h1 { color:red; }", this.response.getContentAsString()); + } + @Test public void invalidPath() throws Exception { for (HttpMethod method : HttpMethod.values()) {