Class AbstractMappingContentNegotiationStrategy
- All Implemented Interfaces:
- ContentNegotiationStrategy,- MediaTypeFileExtensionResolver
- Direct Known Subclasses:
- ParameterContentNegotiationStrategy,- PathExtensionContentNegotiationStrategy
ContentNegotiationStrategy implementations with the
 steps to resolve a request to media types.
 First a key (for example, "json", "pdf") must be extracted from the request (for example,
 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 subclasses to plug in additional
 ways of looking up media types (for example, 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.
- Since:
- 3.2
- Author:
- Rossen Stoyanchev
- 
Field SummaryFieldsFields inherited from interface org.springframework.web.accept.ContentNegotiationStrategyMEDIA_TYPE_ALL_LIST
- 
Constructor SummaryConstructorsConstructorDescriptionAbstractMappingContentNegotiationStrategy(Map<String, MediaType> mediaTypes) Create an instance with the given map of file extensions and media types.
- 
Method SummaryModifier and TypeMethodDescriptionprotected abstract StringgetMediaTypeKey(NativeWebRequest request) Extract a key from the request to use to look up media types.protected voidhandleMatch(String key, MediaType mediaType) Override to provide handling when a key is successfully resolved viaMappingMediaTypeFileExtensionResolver.lookupMediaType(java.lang.String).protected MediaTypehandleNoMatch(NativeWebRequest request, String key) Override to provide handling when a key is not resolved via.booleanbooleanresolveMediaTypeKey(NativeWebRequest webRequest, String key) An alternative toresolveMediaTypes(NativeWebRequest)that accepts an already extracted key.resolveMediaTypes(NativeWebRequest webRequest) Resolve the given request to a list of media types.voidsetIgnoreUnknownExtensions(boolean ignoreUnknownExtensions) Whether to ignore requests with unknown file extension.voidsetUseRegisteredExtensionsOnly(boolean useRegisteredExtensionsOnly) Whether to only use the registered mappings to look up file extensions, or also to use dynamic resolution (for example, viaMediaTypeFactory).Methods inherited from class org.springframework.web.accept.MappingMediaTypeFileExtensionResolveraddMapping, getAllFileExtensions, getAllMediaTypes, getMediaTypes, lookupMediaType, resolveFileExtensions
- 
Field Details- 
logger
 
- 
- 
Constructor Details- 
AbstractMappingContentNegotiationStrategyCreate an instance with the given map of file extensions and media types.
 
- 
- 
Method Details- 
setUseRegisteredExtensionsOnlypublic void setUseRegisteredExtensionsOnly(boolean useRegisteredExtensionsOnly) Whether to only use the registered mappings to look up file extensions, or also to use dynamic resolution (for example, viaMediaTypeFactory).By default this is set to false.
- 
isUseRegisteredExtensionsOnlypublic boolean isUseRegisteredExtensionsOnly()
- 
setIgnoreUnknownExtensionspublic void setIgnoreUnknownExtensions(boolean ignoreUnknownExtensions) Whether to ignore requests with unknown file extension. Setting this tofalseresults inHttpMediaTypeNotAcceptableException.By default this is set to false but is overridden in PathExtensionContentNegotiationStrategyto true.
- 
isIgnoreUnknownExtensionspublic boolean isIgnoreUnknownExtensions()
- 
resolveMediaTypespublic List<MediaType> resolveMediaTypes(NativeWebRequest webRequest) throws HttpMediaTypeNotAcceptableException Description copied from interface:ContentNegotiationStrategyResolve the given request to a list of media types. The returned list is ordered by specificity first and by quality parameter second.- Specified by:
- resolveMediaTypesin interface- ContentNegotiationStrategy
- Parameters:
- webRequest- the current request
- Returns:
- the requested media types, or ContentNegotiationStrategy.MEDIA_TYPE_ALL_LISTif none were requested.
- Throws:
- HttpMediaTypeNotAcceptableException- if the requested media types cannot be parsed
 
- 
resolveMediaTypeKeypublic List<MediaType> resolveMediaTypeKey(NativeWebRequest webRequest, @Nullable String key) throws HttpMediaTypeNotAcceptableException An alternative toresolveMediaTypes(NativeWebRequest)that accepts an already extracted key.- Throws:
- HttpMediaTypeNotAcceptableException
- Since:
- 3.2.16
 
- 
getMediaTypeKeyExtract a key from the request to use to look up media types.- Returns:
- the lookup key, or nullif none
 
- 
handleMatchOverride to provide handling when a key is successfully resolved viaMappingMediaTypeFileExtensionResolver.lookupMediaType(java.lang.String).
- 
handleNoMatch@Nullable 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). Subclasses 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.
 
-