Skip to content

Commit ca09dcb

Browse files
committed
Polish media type change in ResourceHttpRequestHandler
1 parent 198a74d commit ca09dcb

File tree

1 file changed

+13
-19
lines changed

1 file changed

+13
-19
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,8 @@
4848
import org.springframework.util.ObjectUtils;
4949
import org.springframework.util.ResourceUtils;
5050
import org.springframework.util.StringUtils;
51-
import org.springframework.web.HttpMediaTypeNotAcceptableException;
5251
import org.springframework.web.HttpRequestHandler;
5352
import org.springframework.web.accept.ContentNegotiationManager;
54-
import org.springframework.web.accept.ContentNegotiationManagerFactoryBean;
5553
import org.springframework.web.accept.PathExtensionContentNegotiationStrategy;
5654
import org.springframework.web.accept.ServletPathExtensionContentNegotiationStrategy;
5755
import org.springframework.web.context.request.ServletWebRequest;
@@ -216,25 +214,18 @@ public ResourceRegionHttpMessageConverter getResourceRegionHttpMessageConverter(
216214
}
217215

218216
/**
219-
* Configure a {@code ContentNegotiationManager} to determine the media types
220-
* for resources being served. If the manager contains a path
221-
* extension strategy it will be used to look up the file extension
222-
* of resources being served via
223-
* {@link PathExtensionContentNegotiationStrategy#getMediaTypeForResource
224-
* getMediaTypeForResource}. If that fails the check is then expanded
225-
* to use any configured content negotiation strategy against the request.
226-
* <p>By default a {@link ContentNegotiationManagerFactoryBean} with default
227-
* settings is used to create the manager. See the Javadoc of
228-
* {@code ContentNegotiationManagerFactoryBean} for details
229-
* @param contentNegotiationManager the manager to use
217+
* Configure a {@code ContentNegotiationManager} to help determine the
218+
* media types for resources being served. If the manager contains a path
219+
* extension strategy it will be checked for registered file extension.
220+
* @param contentNegotiationManager the manager in use
230221
* @since 4.3
231222
*/
232223
public void setContentNegotiationManager(ContentNegotiationManager contentNegotiationManager) {
233224
this.contentNegotiationManager = contentNegotiationManager;
234225
}
235226

236227
/**
237-
* Return the specified content negotiation manager.
228+
* Return the configured content negotiation manager.
238229
* @since 4.3
239230
*/
240231
public ContentNegotiationManager getContentNegotiationManager() {
@@ -516,17 +507,20 @@ protected boolean isInvalidPath(String path) {
516507

517508
/**
518509
* Determine the media type for the given request and the resource matched
519-
* to it. This implementation first tries to determine the MediaType based
520-
* strictly on the file extension of the Resource via
521-
* {@link PathExtensionContentNegotiationStrategy#getMediaTypeForResource}
522-
* and then expands to check against the request via
523-
* {@link ContentNegotiationManager#resolveMediaTypes}.
510+
* to it. This implementation tries to determine the MediaType based on the
511+
* file extension of the Resource via
512+
* {@link ServletPathExtensionContentNegotiationStrategy#getMediaTypeForResource}.
524513
* @param request the current request
525514
* @param resource the resource to check
526515
* @return the corresponding media type, or {@code null} if none found
527516
*/
528517
@SuppressWarnings("deprecation")
529518
protected MediaType getMediaType(HttpServletRequest request, Resource resource) {
519+
// For backwards compatibility
520+
MediaType mediaType = getMediaType(resource);
521+
if (mediaType != null) {
522+
return mediaType;
523+
}
530524
return this.pathExtensionStrategy.getMediaTypeForResource(resource);
531525
}
532526

0 commit comments

Comments
 (0)