public class PathExtensionContentTypeResolver extends AbstractMappingContentTypeResolver
RequestedContentTypeResolver
that extracts the file extension from
the request path and uses that as the media type lookup key.
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.
Modifier and Type | Field and Description |
---|---|
private boolean |
ignoreUnknownExtensions |
private boolean |
useJaf |
Constructor and Description |
---|
PathExtensionContentTypeResolver()
Create an instance without any mappings to start with.
|
PathExtensionContentTypeResolver(java.util.Map<java.lang.String,MediaType> mediaTypes)
Create an instance with the given map of file extensions and media types.
|
Modifier and Type | Method and Description |
---|---|
protected java.lang.String |
extractKey(ServerWebExchange exchange)
Extract the key to use to look up a media type from the given exchange,
e.g.
|
protected MediaType |
handleNoMatch(java.lang.String key)
Override to provide handling when a key is not resolved via.
|
MediaType |
resolveMediaTypeForResource(Resource resource)
A public method exposing the knowledge of the path extension resolver to
determine the media type for a given
Resource . |
void |
setIgnoreUnknownExtensions(boolean ignoreUnknownExtensions)
Whether to ignore requests with unknown file extension.
|
void |
setUseJaf(boolean useJaf)
Whether to use the Java Activation Framework to look up file extensions.
|
getAllMediaTypes, getKeys, getKeysFor, getMediaType, getMediaTypes, handleMatch, resolveMediaTypes, resolveMediaTypes
private boolean useJaf
private boolean ignoreUnknownExtensions
public PathExtensionContentTypeResolver(java.util.Map<java.lang.String,MediaType> mediaTypes)
public PathExtensionContentTypeResolver()
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 java.lang.String extractKey(ServerWebExchange exchange)
AbstractMappingContentTypeResolver
extractKey
in class AbstractMappingContentTypeResolver
null
protected MediaType handleNoMatch(java.lang.String key) throws NotAcceptableStatusException
AbstractMappingContentTypeResolver
AbstractMappingContentTypeResolver.getMediaType(String)
. If a MediaType is returned from
this method it will be added to the mappings.handleNoMatch
in class AbstractMappingContentTypeResolver
NotAcceptableStatusException
public MediaType resolveMediaTypeForResource(Resource resource)
Resource
. First it checks
the explicitly registered mappings and then falls back on JAF.resource
- the resourcenull
if none determined