public class PathExtensionContentNegotiationStrategy extends AbstractMappingContentNegotiationStrategy
ContentNegotiationStrategy
that resolves the file extension in the
request path to a key to be used to look up a media type.
If the file extension is not found in the explicit registrations provided to the constructor, the Java Activation Framework (JAF) is used as a fallback mechanism.
The presence of the JAF is detected and enabled automatically but the
setUseJaf(boolean)
property may be set to false.
Constructor and Description |
---|
PathExtensionContentNegotiationStrategy()
Create an instance without any mappings to start with.
|
PathExtensionContentNegotiationStrategy(Map<String,MediaType> mediaTypes)
Create an instance with the given map of file extensions and media types.
|
Modifier and Type | Method and Description |
---|---|
MediaType |
getMediaTypeForResource(Resource resource)
|
protected String |
getMediaTypeKey(NativeWebRequest webRequest)
Extract a key from the request to use to look up media types.
|
protected MediaType |
handleNoMatch(NativeWebRequest webRequest,
String extension)
Override to provide handling when a key is not resolved via.
|
void |
setIgnoreUnknownExtensions(boolean ignoreUnknownExtensions)
Whether to ignore requests with unknown file extension.
|
void |
setUrlPathHelper(UrlPathHelper urlPathHelper)
Configure a
UrlPathHelper to use in getMediaTypeKey(org.springframework.web.context.request.NativeWebRequest)
in order to derive the lookup path for a target request URL path. |
void |
setUseJaf(boolean useJaf)
Whether to use the Java Activation Framework to look up file extensions.
|
handleMatch, resolveMediaTypeKey, resolveMediaTypes
addMapping, getAllFileExtensions, getAllMediaTypes, getMediaTypes, lookupMediaType, resolveFileExtensions
public PathExtensionContentNegotiationStrategy()
public void setUrlPathHelper(UrlPathHelper urlPathHelper)
UrlPathHelper
to use in getMediaTypeKey(org.springframework.web.context.request.NativeWebRequest)
in order to derive the lookup path for a target request URL path.public void setUseJaf(boolean useJaf)
By default this is set to "true" but depends on JAF being present.
public void setIgnoreUnknownExtensions(boolean ignoreUnknownExtensions)
false
results in HttpMediaTypeNotAcceptableException
.
By default this is set to true
.
protected String getMediaTypeKey(NativeWebRequest webRequest)
AbstractMappingContentNegotiationStrategy
getMediaTypeKey
in class AbstractMappingContentNegotiationStrategy
null
.protected MediaType handleNoMatch(NativeWebRequest webRequest, String extension) throws HttpMediaTypeNotAcceptableException
AbstractMappingContentNegotiationStrategy
MappingMediaTypeFileExtensionResolver.lookupMediaType(java.lang.String)
. Sub-classes can take further steps to
determine the media type(s). If a MediaType is returned from
this method it will be added to the cache in the base class.handleNoMatch
in class AbstractMappingContentNegotiationStrategy
HttpMediaTypeNotAcceptableException
public MediaType getMediaTypeForResource(Resource resource)
MediaType
in this case for a given
Resource
. The method first looks up any explicitly registered
file extensions first and then falls back on JAF if available.resource
- the resource to look upnull
if none found