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

This class is used to determine the requested media types of a request by delegating to a list of ContentNegotiationStrategy instances. The strategies must be provided at instantiation or alternatively if using the default constructor, an instance of `HeaderContentNegotiationStrategy` will be configured by default.

This class may also be used to look up file extensions associated with a MediaType. This is done by consulting the list of configured `MediaTypeFileExtensionResolver` instances. Note that some ContentNegotiationStrategy implementations also implement MediaTypeFileExtensionResolver and the class constructor accepting the former will also detect if they implement the latter. If you need to register additional resolvers, you can use the method `addFileExtensionResolvers(MediaTypeFileExtensionResolver...)`.

Since:
  3.2
Author:
  Rossen Stoyanchev

### Constructor Summary

Constructors
Constructor and Description
`ContentNegotiationManager()` Create an instance with a `HeaderContentNegotiationStrategy`.
`ContentNegotiationManager(ContentNegotiationStrategy... strategies)` Create an instance with the given ContentNegotiationStrategy instances.

### Method Summary

Methods
`void` `addFileExtensionResolvers(MediaTypeFileExtensionResolver... resolvers)` Add MediaTypeFileExtensionResolver instances.
`List<String>` `getAllFileExtensions()` Delegate to all configured MediaTypeFileExtensionResolver instances and aggregate the list of all known file extensions.
`List<String>` `resolveFileExtensions(MediaType mediaType)` Delegate to all configured MediaTypeFileExtensionResolver instances and aggregate the list of all file extensions found.
`List<MediaType>` `resolveMediaTypes(NativeWebRequest webRequest)` Delegate to all configured ContentNegotiationStrategy instances until one returns a non-empty list.

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

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

### Constructor Detail

#### ContentNegotiationManager

    public ContentNegotiationManager(ContentNegotiationStrategy... strategies)

Create an instance with the given ContentNegotiationStrategy instances.

Each instance is checked to see if it is also an implementation of MediaTypeFileExtensionResolver, and if so it is registered as such.

Parameters:
  `strategies` - one more more ContentNegotiationStrategy instances

#### ContentNegotiationManager

    public ContentNegotiationManager()

Create an instance with a `HeaderContentNegotiationStrategy`.

### Method Detail

#### addFileExtensionResolvers

    public void addFileExtensionResolvers(MediaTypeFileExtensionResolver... resolvers)

Add MediaTypeFileExtensionResolver instances.

Note that some `ContentNegotiationStrategy` implementations also implement `MediaTypeFileExtensionResolver` and the class constructor accepting the former will also detect implementations of the latter. Therefore you only need to use this method to register additional instances.

Parameters:
  `resolvers` - one or more resolvers

#### resolveMediaTypes

    public List<MediaType> resolveMediaTypes(NativeWebRequest webRequest)
                                      throws HttpMediaTypeNotAcceptableException

Delegate to all configured ContentNegotiationStrategy instances until one returns a non-empty list.

Specified by:
  `resolveMediaTypes` in interface `ContentNegotiationStrategy`
Parameters:
  `webRequest` - 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)

Delegate to all configured MediaTypeFileExtensionResolver instances and aggregate the list of all file extensions found.

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()

Delegate to all configured MediaTypeFileExtensionResolver instances and aggregate the list of all known file extensions.

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