Package org.springframework.web.accept
Class ContentNegotiationManager
- java.lang.Object
- org.springframework.web.accept.ContentNegotiationManager
- All Implemented Interfaces:
ContentNegotiationStrategy
,MediaTypeFileExtensionResolver
public class ContentNegotiationManager extends Object implements ContentNegotiationStrategy, MediaTypeFileExtensionResolver
Central class to determine requested media types for a request. This is done by delegating to a list of configuredContentNegotiationStrategy
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.- Since:
- 3.2
- Author:
- Rossen Stoyanchev, Juergen Hoeller
Constructor Summary
Constructors Constructor Description ContentNegotiationManager()
Create a default instance with aHeaderContentNegotiationStrategy
.ContentNegotiationManager(Collection<ContentNegotiationStrategy> strategies)
A collection-based alternative toContentNegotiationManager(ContentNegotiationStrategy...)
.ContentNegotiationManager(ContentNegotiationStrategy... strategies)
Create an instance with the given list ofContentNegotiationStrategy
strategies each of which may also be an instance ofMediaTypeFileExtensionResolver
.
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addFileExtensionResolvers(MediaTypeFileExtensionResolver... resolvers)
Register moreMediaTypeFileExtensionResolver
instances in addition to those detected at construction.List<String>
getAllFileExtensions()
Return all registered file extensions.List<ContentNegotiationStrategy>
getStrategies()
Return the configured content negotiation strategies.<T extends ContentNegotiationStrategy>
TgetStrategy(Class<T> strategyType)
Find aContentNegotiationStrategy
of the given type.List<String>
resolveFileExtensions(MediaType mediaType)
Resolve the given media type to a list of path extensions.List<MediaType>
resolveMediaTypes(NativeWebRequest request)
Resolve the given request to a list of media types.
Constructor Detail
ContentNegotiationManager
public ContentNegotiationManager(ContentNegotiationStrategy... strategies)
Create an instance with the given list ofContentNegotiationStrategy
strategies each of which may also be an instance ofMediaTypeFileExtensionResolver
.- Parameters:
strategies
- the strategies to use
ContentNegotiationManager
public ContentNegotiationManager(Collection<ContentNegotiationStrategy> strategies)
A collection-based alternative toContentNegotiationManager(ContentNegotiationStrategy...)
.- Parameters:
strategies
- the strategies to use- Since:
- 3.2.2
ContentNegotiationManager
public ContentNegotiationManager()
Create a default instance with aHeaderContentNegotiationStrategy
.
Method Detail
getStrategies
public List<ContentNegotiationStrategy> getStrategies()
Return the configured content negotiation strategies.- Since:
- 3.2.16
getStrategy
public <T extends ContentNegotiationStrategy> T getStrategy(Class<T> strategyType)
Find aContentNegotiationStrategy
of the given type.- Parameters:
strategyType
- the strategy type- Returns:
- the first matching strategy, or
null
if none - Since:
- 4.3
addFileExtensionResolvers
public void addFileExtensionResolvers(MediaTypeFileExtensionResolver... resolvers)
Register moreMediaTypeFileExtensionResolver
instances in addition to those detected at construction.- Parameters:
resolvers
- the resolvers to add
resolveMediaTypes
public List<MediaType> resolveMediaTypes(NativeWebRequest request) 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:
request
- the current request- Returns:
- the requested media types or an empty list (never
null
) - Throws:
HttpMediaTypeNotAcceptableException
- if the requested media types cannot be parsed
resolveFileExtensions
public List<String> resolveFileExtensions(MediaType mediaType)
Description copied from interface:MediaTypeFileExtensionResolver
Resolve the given media type to a list of path extensions.- Specified by:
resolveFileExtensions
in interfaceMediaTypeFileExtensionResolver
- Parameters:
mediaType
- the media type to resolve- Returns:
- a list of extensions or an empty list (never
null
)
getAllFileExtensions
public List<String> getAllFileExtensions()
Return all registered file extensions.At startup this method returns extensions explicitly registered with either
PathExtensionContentNegotiationStrategy
orParameterContentNegotiationStrategy
. At runtime if there is a "path extension" strategy and itsuseJaf
property is set to "true", the list of extensions may increase as file extensions are resolved via JAF and cached.- Specified by:
getAllFileExtensions
in interfaceMediaTypeFileExtensionResolver
- Returns:
- a list of extensions or an empty list (never
null
)