Package org.springframework.web.accept
Class AbstractMappingContentNegotiationStrategy
- java.lang.Object
- org.springframework.web.accept.MappingMediaTypeFileExtensionResolver
- org.springframework.web.accept.AbstractMappingContentNegotiationStrategy
- All Implemented Interfaces:
ContentNegotiationStrategy
,MediaTypeFileExtensionResolver
- Direct Known Subclasses:
ParameterContentNegotiationStrategy
,PathExtensionContentNegotiationStrategy
public abstract class AbstractMappingContentNegotiationStrategy extends MappingMediaTypeFileExtensionResolver implements ContentNegotiationStrategy
Base class forContentNegotiationStrategy
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, orServletContext.getMimeType(java.lang.String)
. Media types resolved via base classes are then added to the base classMappingMediaTypeFileExtensionResolver
, i.e. cached for new lookups.- Since:
- 3.2
- Author:
- Rossen Stoyanchev
Constructor Summary
Constructors Constructor Description AbstractMappingContentNegotiationStrategy(Map<String,MediaType> mediaTypes)
Create an instance with the given map of file extensions and media types.
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected abstract String
getMediaTypeKey(NativeWebRequest request)
Extract a key from the request to use to look up media types.protected void
handleMatch(String key, MediaType mediaType)
Override to provide handling when a key is successfully resolved viaMappingMediaTypeFileExtensionResolver.lookupMediaType(java.lang.String)
.protected MediaType
handleNoMatch(NativeWebRequest request, String key)
Override to provide handling when a key is not resolved via.List<MediaType>
resolveMediaTypeKey(NativeWebRequest webRequest, String key)
An alternative toresolveMediaTypes(NativeWebRequest)
that accepts an already extracted key.List<MediaType>
resolveMediaTypes(NativeWebRequest webRequest)
Resolve the given request to a list of media types.Methods inherited from class org.springframework.web.accept.MappingMediaTypeFileExtensionResolver
addMapping, getAllFileExtensions, getAllMediaTypes, getMediaTypes, lookupMediaType, resolveFileExtensions
Constructor Detail
AbstractMappingContentNegotiationStrategy
public AbstractMappingContentNegotiationStrategy(Map<String,MediaType> mediaTypes)
Create an instance with the given map of file extensions and media types.
Method Detail
resolveMediaTypes
public List<MediaType> resolveMediaTypes(NativeWebRequest webRequest) throws HttpMediaTypeNotAcceptableException
Description copied from interface:ContentNegotiationStrategy
Resolve the given request to a list of media types. The returned list is ordered by specificity first and by quality parameter second.- Specified by:
resolveMediaTypes
in interfaceContentNegotiationStrategy
- Parameters:
webRequest
- the current request- Returns:
- the requested media types or an empty list (never
null
) - Throws:
HttpMediaTypeNotAcceptableException
- if the requested media types cannot be parsed
resolveMediaTypeKey
public List<MediaType> resolveMediaTypeKey(NativeWebRequest webRequest, String key) throws HttpMediaTypeNotAcceptableException
An alternative toresolveMediaTypes(NativeWebRequest)
that accepts an already extracted key.- Throws:
HttpMediaTypeNotAcceptableException
- Since:
- 3.2.16
getMediaTypeKey
protected abstract String getMediaTypeKey(NativeWebRequest request)
Extract a key from the request to use to look up media types.- Returns:
- the lookup key, or
null
if none
handleMatch
protected void handleMatch(String key, MediaType mediaType)
Override to provide handling when a key is successfully resolved viaMappingMediaTypeFileExtensionResolver.lookupMediaType(java.lang.String)
.
handleNoMatch
protected MediaType handleNoMatch(NativeWebRequest request, String key) throws HttpMediaTypeNotAcceptableException
Override to provide handling when a key is not resolved via.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.