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 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.

Since:
  3.2
Author:
  Rossen Stoyanchev, Juergen Hoeller

## Field Summary

### Fields inherited from interface org.springframework.web.accept.ContentNegotiationStrategy

`MEDIA_TYPE_ALL_LIST`

## Constructor Summary

Constructors
Constructor
Description
`ContentNegotiationManager()`
Create a default instance with a `HeaderContentNegotiationStrategy`.
`ContentNegotiationManager(Collection<ContentNegotiationStrategy> strategies)`
A collection-based alternative to `ContentNegotiationManager(ContentNegotiationStrategy...)`.
`ContentNegotiationManager(ContentNegotiationStrategy... strategies)`
Create an instance with the given list of `ContentNegotiationStrategy` strategies each of which may also be an instance of `MediaTypeFileExtensionResolver`.

## Method Summary

Modifier and Type
Method
Description
`void`
`addFileExtensionResolvers(MediaTypeFileExtensionResolver... resolvers)`
Register more `MediaTypeFileExtensionResolver` instances in addition to those detected at construction.
`List<String>`
`getAllFileExtensions()`
Get all registered file extensions.
`Map<String,MediaType>`
`getMediaTypeMappings()`
Return all registered lookup key to media type mappings by iterating `MediaTypeFileExtensionResolver`s.
`List<ContentNegotiationStrategy>`
`getStrategies()`
Return the configured content negotiation strategies.
`<T extends ContentNegotiationStrategy>
T`
`getStrategy(Class<T> strategyType)`
Find a `ContentNegotiationStrategy` of the given type.
`List<String>`
`resolveFileExtensions(MediaType mediaType)`
Resolve the given media type to a list of file extensions.
`List<MediaType>`
`resolveMediaTypes(NativeWebRequest request)`
Resolve the given request to a list of media types.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Constructor Details

### ContentNegotiationManager

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` - the strategies to use

### ContentNegotiationManager

public ContentNegotiationManager(Collection<ContentNegotiationStrategy> strategies)
A collection-based alternative to `ContentNegotiationManager(ContentNegotiationStrategy...)`.

Parameters:
  `strategies` - the strategies to use
Since:
  3.2.2

### ContentNegotiationManager

public ContentNegotiationManager()
Create a default instance with a `HeaderContentNegotiationStrategy`.

## Method Details

### getStrategies

public List<ContentNegotiationStrategy> getStrategies()
Return the configured content negotiation strategies.

Since:
  3.2.16

### getStrategy

@Nullable public <T extends ContentNegotiationStrategy> T getStrategy(Class<T> strategyType)
Find a `ContentNegotiationStrategy` 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 more `MediaTypeFileExtensionResolver` 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 interface `ContentNegotiationStrategy`
Parameters:
  `request` - the current request
Returns:
  the requested media types, or `ContentNegotiationStrategy.MEDIA_TYPE_ALL_LIST` if none were requested.
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 file extensions.

Specified by:
  `resolveFileExtensions` in interface `MediaTypeFileExtensionResolver`
Parameters:
  `mediaType` - the media type to resolve
Returns:
  a list of extensions or an empty list (never `null`)

### getAllFileExtensions

public List<String> 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 `MediaTypeFactory` and cached.

Specified by:
  `getAllFileExtensions` in interface `MediaTypeFileExtensionResolver`
Returns:
  a list of extensions or an empty list (never `null`)

### getMediaTypeMappings

public Map<String,MediaType> getMediaTypeMappings()
Return all registered lookup key to media type mappings by iterating `MediaTypeFileExtensionResolver`s.

Since:
  5.2.4