public abstract class AbstractMappingContentNegotiationStrategy extends MappingMediaTypeFileExtensionResolver implements ContentNegotiationStrategy
ContentNegotiationStrategy
implementations with the
steps to resolve a request to media types.
First a key (e.g. "json", "pdf") must be extracted from the request (e.g.
file extension, query param). The key must then be resolved to media type(s)
through the base class MappingMediaTypeFileExtensionResolver
which
stores such mappings.
The method handleNoMatch(org.springframework.web.context.request.NativeWebRequest, java.lang.String)
allow sub-classes to plug in additional
ways of looking up media types (e.g. through the Java Activation framework,
or ServletContext.getMimeType(java.lang.String)
. Media types resolved
via base classes are then added to the base class
MappingMediaTypeFileExtensionResolver
, i.e. cached for new lookups.
Modifier and Type | Field and Description |
---|---|
protected Log |
logger |
MEDIA_TYPE_ALL_LIST
Constructor and Description |
---|
AbstractMappingContentNegotiationStrategy(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 abstract java.lang.String |
getMediaTypeKey(NativeWebRequest request)
Extract a key from the request to use to look up media types.
|
protected void |
handleMatch(java.lang.String key,
MediaType mediaType)
Override to provide handling when a key is successfully resolved via
MappingMediaTypeFileExtensionResolver.lookupMediaType(java.lang.String) . |
protected MediaType |
handleNoMatch(NativeWebRequest request,
java.lang.String key)
Override to provide handling when a key is not resolved via.
|
boolean |
isIgnoreUnknownExtensions() |
boolean |
isUseRegisteredExtensionsOnly() |
java.util.List<MediaType> |
resolveMediaTypeKey(NativeWebRequest webRequest,
java.lang.String key)
An alternative to
resolveMediaTypes(NativeWebRequest) that accepts
an already extracted key. |
java.util.List<MediaType> |
resolveMediaTypes(NativeWebRequest webRequest)
Resolve the given request to a list of media types.
|
void |
setIgnoreUnknownExtensions(boolean ignoreUnknownExtensions)
Whether to ignore requests with unknown file extension.
|
void |
setUseRegisteredExtensionsOnly(boolean useRegisteredExtensionsOnly)
Whether to only use the registered mappings to look up file extensions,
or also to use dynamic resolution (e.g.
|
addMapping, getAllFileExtensions, getAllMediaTypes, getMediaTypes, lookupMediaType, resolveFileExtensions
protected final Log logger
public void setUseRegisteredExtensionsOnly(boolean useRegisteredExtensionsOnly)
MediaTypeFactory
.
By default this is set to false
.
public boolean isUseRegisteredExtensionsOnly()
public void setIgnoreUnknownExtensions(boolean ignoreUnknownExtensions)
false
results in HttpMediaTypeNotAcceptableException
.
By default this is set to false but is overridden in
PathExtensionContentNegotiationStrategy
to true.
public boolean isIgnoreUnknownExtensions()
public java.util.List<MediaType> resolveMediaTypes(NativeWebRequest webRequest) throws HttpMediaTypeNotAcceptableException
ContentNegotiationStrategy
resolveMediaTypes
in interface ContentNegotiationStrategy
webRequest
- the current requestContentNegotiationStrategy.MEDIA_TYPE_ALL_LIST
if none
were requested.HttpMediaTypeNotAcceptableException
- if the requested media
types cannot be parsedpublic java.util.List<MediaType> resolveMediaTypeKey(NativeWebRequest webRequest, @Nullable java.lang.String key) throws HttpMediaTypeNotAcceptableException
resolveMediaTypes(NativeWebRequest)
that accepts
an already extracted key.HttpMediaTypeNotAcceptableException
@Nullable protected abstract java.lang.String getMediaTypeKey(NativeWebRequest request)
null
if noneprotected void handleMatch(java.lang.String key, MediaType mediaType)
MappingMediaTypeFileExtensionResolver.lookupMediaType(java.lang.String)
.@Nullable protected MediaType handleNoMatch(NativeWebRequest request, java.lang.String key) throws HttpMediaTypeNotAcceptableException
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.