Top Description Fields Constructors Methods
org.springframework.web.accept

public abstract Class AbstractMappingContentNegotiationStrategy

extends MappingMediaTypeFileExtensionResolver
implements ContentNegotiationStrategy
Class Inheritance
All Implemented Interfaces
org.springframework.web.accept.ContentNegotiationStrategy
Known Direct Subclasses
org.springframework.web.accept.ParameterContentNegotiationStrategy, org.springframework.web.accept.PathExtensionContentNegotiationStrategy
Imports
java.util.Collections, .List, .Map, .Optional, org.apache.commons.logging.Log, .LogFactory, org.springframework.http.MediaType, .MediaTypeFactory, org.springframework.lang.Nullable, org.springframework.util.StringUtils, org.springframework.web.HttpMediaTypeNotAcceptableException, org.springframework.web.context.request.NativeWebRequest

Base class for 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 allow subclasses to plug in additional ways of looking up media types (e.g. through the Java Activation framework, or jakarta.servlet.ServletContext#getMimeType). Media types resolved via base classes are then added to the base class MappingMediaTypeFileExtensionResolver, i.e. cached for new lookups.

Author
Rossen Stoyanchev
Since
3.2

Field Summary

Modifier and TypeField and Description
private boolean
protected final Log
private boolean

Constructor Summary

AccessConstructor and Description
public
AbstractMappingContentNegotiationStrategy(Map<String, MediaType> mediaTypes)

Create an instance with the given map of file extensions and media types.

Method Summary

Modifier and TypeMethod and Description
protected abstract String

Returns:

the lookup key, or null if none
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 via lookupMediaType.

protected MediaType
handleNoMatch(NativeWebRequest request, String key)

Override to provide handling when a key is not resolved via.

public boolean
public boolean
public List<MediaType>
resolveMediaTypeKey(NativeWebRequest webRequest, String key)

An alternative to resolveMediaTypes(NativeWebRequest) that accepts an already extracted key.

public List<MediaType>
resolveMediaTypes(NativeWebRequest
the current request
webRequest
)

Implements org.springframework.web.accept.ContentNegotiationStrategy.resolveMediaTypes.

Resolve the given request to a list of media types.
public void
setIgnoreUnknownExtensions(boolean ignoreUnknownExtensions)

Whether to ignore requests with unknown file extension.

public void
setUseRegisteredExtensionsOnly(boolean useRegisteredExtensionsOnly)

Whether to only use the registered mappings to look up file extensions, or also to use dynamic resolution (e.g. via MediaTypeFactory).

Inherited from org.springframework.web.accept.MappingMediaTypeFileExtensionResolver:
addMappinggetAllFileExtensionsgetAllMediaTypesgetMediaTypeslookupMediaTyperesolveFileExtensions

Field Detail

ignoreUnknownExtensionsback to summary
private boolean ignoreUnknownExtensions
loggerback to summary
protected final Log logger
useRegisteredExtensionsOnlyback to summary
private boolean useRegisteredExtensionsOnly

Constructor Detail

AbstractMappingContentNegotiationStrategyback to summary
public AbstractMappingContentNegotiationStrategy(Map<String, MediaType> mediaTypes)

Create an instance with the given map of file extensions and media types.

Method Detail

getMediaTypeKeyback to summary
protected abstract String getMediaTypeKey(NativeWebRequest request)

Extract a key from the request to use to look up media types.

Returns:String

the lookup key, or null if none

Annotations
@Nullable
handleMatchback to summary
protected void handleMatch(String key, MediaType mediaType)

Override to provide handling when a key is successfully resolved via lookupMediaType.

handleNoMatchback to summary
protected MediaType handleNoMatch(NativeWebRequest request, String key) throws HttpMediaTypeNotAcceptableException

Override to provide handling when a key is not resolved via. lookupMediaType. 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.

Annotations
@Nullable
isIgnoreUnknownExtensionsback to summary
public boolean isIgnoreUnknownExtensions()
isUseRegisteredExtensionsOnlyback to summary
public boolean isUseRegisteredExtensionsOnly()
resolveMediaTypeKeyback to summary
public List<MediaType> resolveMediaTypeKey(NativeWebRequest webRequest, String key) throws HttpMediaTypeNotAcceptableException

An alternative to resolveMediaTypes(NativeWebRequest) that accepts an already extracted key.

Since
3.2.16
resolveMediaTypesback to summary
public List<MediaType> resolveMediaTypes(NativeWebRequest webRequest) throws HttpMediaTypeNotAcceptableException

Implements org.springframework.web.accept.ContentNegotiationStrategy.resolveMediaTypes.

Doc from org.springframework.web.accept.ContentNegotiationStrategy.resolveMediaTypes.

Resolve the given request to a list of media types. The returned list is ordered by specificity first and by quality parameter second.

Parameters
webRequest:NativeWebRequest

the current request

Returns:List<MediaType>

the requested media types, or MEDIA_TYPE_ALL_LIST if none were requested.

Annotations
@Override
Exceptions
HttpMediaTypeNotAcceptableException:
if the requested media types cannot be parsed
setIgnoreUnknownExtensionsback to summary
public void setIgnoreUnknownExtensions(boolean ignoreUnknownExtensions)

Whether to ignore requests with unknown file extension. Setting this to false results in HttpMediaTypeNotAcceptableException.

By default this is set to false but is overridden in PathExtensionContentNegotiationStrategy to true.

setUseRegisteredExtensionsOnlyback to summary
public void setUseRegisteredExtensionsOnly(boolean useRegisteredExtensionsOnly)

Whether to only use the registered mappings to look up file extensions, or also to use dynamic resolution (e.g. via MediaTypeFactory).

By default this is set to false.