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

public Class ContentNegotiationManager

extends Object
implements ContentNegotiationStrategy, MediaTypeFileExtensionResolver
Class Inheritance
All Implemented Interfaces
org.springframework.web.accept.MediaTypeFileExtensionResolver, org.springframework.web.accept.ContentNegotiationStrategy
Imports
java.util.ArrayList, .Arrays, .Collection, .Collections, .HashMap, .LinkedHashSet, .List, .Map, .Set, java.util.function.Function, org.springframework.http.MediaType, org.springframework.lang.Nullable, org.springframework.util.Assert, .CollectionUtils, org.springframework.web.HttpMediaTypeNotAcceptableException, org.springframework.web.context.request.NativeWebRequest

Central class to determine requested media types for a request. This is done by delegating to a list of configured ContentNegotiationStrategy instances.

Also provides methods to look up file extensions for a media type. This is done by delegating to the list of configured MediaTypeFileExtensionResolver instances.

Authors
Rossen Stoyanchev, Juergen Hoeller
Since
3.2

Field Summary

Modifier and TypeField and Description
private final Set<MediaTypeFileExtensionResolver>
private final List<ContentNegotiationStrategy>

Constructor Summary

AccessConstructor and Description
public
ContentNegotiationManager(ContentNegotiationStrategy...
the strategies to use
strategies
)

Create an instance with the given list of ContentNegotiationStrategy strategies each of which may also be an instance of MediaTypeFileExtensionResolver.

public
public

Method Summary

Modifier and TypeMethod and Description
public void
addFileExtensionResolvers(MediaTypeFileExtensionResolver...
the resolvers to add
resolvers
)

Register more MediaTypeFileExtensionResolver instances in addition to those detected at construction.

private List<String>
public List<String>
public Map<String, MediaType>
getMediaTypeMappings()

Return all registered lookup key to media type mappings by iterating MediaTypeFileExtensionResolvers.

public List<ContentNegotiationStrategy>
getStrategies()

Return the configured content negotiation strategies.

public <T extends ContentNegotiationStrategy> T

Returns:

the first matching strategy, or null if none
getStrategy
(Class<T>
the strategy type
strategyType
)

Find a ContentNegotiationStrategy of the given type.

public List<String>
resolveFileExtensions(MediaType
the media type to resolve
mediaType
)

Implements org.springframework.web.accept.MediaTypeFileExtensionResolver.resolveFileExtensions.

Resolve the given media type to a list of file extensions.

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

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

Resolve the given request to a list of media types.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

resolversback to summary
private final Set<MediaTypeFileExtensionResolver> resolvers
strategiesback to summary
private final List<ContentNegotiationStrategy> strategies

Constructor Detail

ContentNegotiationManagerback to summary
public ContentNegotiationManager(ContentNegotiationStrategy... strategies)

Create an instance with the given list of ContentNegotiationStrategy strategies each of which may also be an instance of MediaTypeFileExtensionResolver.

Parameters
strategies:ContentNegotiationStrategy[]

the strategies to use

ContentNegotiationManagerback to summary
public ContentNegotiationManager(Collection<ContentNegotiationStrategy> strategies)

A collection-based alternative to ContentNegotiationManager(ContentNegotiationStrategy...).

Parameters
strategies:Collection<ContentNegotiationStrategy>

the strategies to use

Since
3.2.2
ContentNegotiationManagerback to summary
public ContentNegotiationManager()

Create a default instance with a HeaderContentNegotiationStrategy.

Method Detail

addFileExtensionResolversback to summary
public void addFileExtensionResolvers(MediaTypeFileExtensionResolver... resolvers)

Register more MediaTypeFileExtensionResolver instances in addition to those detected at construction.

Parameters
resolvers:MediaTypeFileExtensionResolver[]

the resolvers to add

doResolveExtensionsback to summary
private List<String> doResolveExtensions(Function<MediaTypeFileExtensionResolver, List<String>> extractor)
getAllFileExtensionsback to summary
public List<String> getAllFileExtensions()

Implements org.springframework.web.accept.MediaTypeFileExtensionResolver.getAllFileExtensions.

Doc from org.springframework.web.accept.MediaTypeFileExtensionResolver.getAllFileExtensions.

Get all registered file extensions.

At startup this method returns extensions explicitly registered with either PathExtensionContentNegotiationStrategy or ParameterContentNegotiationStrategy. At runtime if there is a "path extension" strategy and its useRegisteredExtensionsOnly property is set to "false", the list of extensions may increase as file extensions are resolved via org.springframework.http.MediaTypeFactory and cached.

Returns:List<String>

a list of extensions or an empty list (never null)

Annotations
@Override
getMediaTypeMappingsback to summary
public Map<String, MediaType> getMediaTypeMappings()

Return all registered lookup key to media type mappings by iterating MediaTypeFileExtensionResolvers.

Since
5.2.4
getStrategiesback to summary
public List<ContentNegotiationStrategy> getStrategies()

Return the configured content negotiation strategies.

Since
3.2.16
getStrategyback to summary
public <T extends ContentNegotiationStrategy> T getStrategy(Class<T> strategyType)

Find a ContentNegotiationStrategy of the given type.

Parameters
strategyType:Class<T>

the strategy type

Returns:T

the first matching strategy, or null if none

Annotations
@SuppressWarnings:unchecked
@Nullable
Since
4.3
resolveFileExtensionsback to summary
public List<String> resolveFileExtensions(MediaType mediaType)

Implements org.springframework.web.accept.MediaTypeFileExtensionResolver.resolveFileExtensions.

Doc from org.springframework.web.accept.MediaTypeFileExtensionResolver.resolveFileExtensions.

Resolve the given media type to a list of file extensions.

Parameters
mediaType:MediaType

the media type to resolve

Returns:List<String>

a list of extensions or an empty list (never null)

Annotations
@Override
resolveMediaTypesback to summary
public List<MediaType> resolveMediaTypes(NativeWebRequest request) 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
request: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