Package org.springframework.web.servlet.config.annotation

# Class ContentNegotiationConfigurer

java.lang.Object
org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer

public class ContentNegotiationConfigurer extends Object
Creates a `ContentNegotiationManager` and configures it with one or more `ContentNegotiationStrategy` instances.

This factory offers properties that in turn result in configuring the underlying strategies. The table below shows the property names, their default settings, as well as the strategies that they help to configure:

Property Setter | Default Value | Underlying Strategy | Enabled Or Not
--- | --- | --- | ---
`favorParameter(boolean)` | false | `ParameterContentNegotiationStrategy` | Off
`favorPathExtension(boolean)` | false | `PathExtensionContentNegotiationStrategy` | Off
`ignoreAcceptHeader(boolean)` | false | `HeaderContentNegotiationStrategy` | Enabled
`defaultContentType(org.springframework.http.MediaType...)` | null | `FixedContentNegotiationStrategy` | Off
`defaultContentTypeStrategy(org.springframework.web.accept.ContentNegotiationStrategy)` | null | `ContentNegotiationStrategy` | Off

As of 5.0 you can set the exact strategies to use via `strategies(List)`.

Note: if you must use URL-based content type resolution, the use of a query parameter is simpler and preferable to the use of a path extension since the latter can cause issues with URI variables, path parameters, and URI decoding. Consider setting `favorPathExtension(boolean)` to false or otherwise set the strategies to use explicitly via `strategies(List)`.

Since:
  3.2
Author:
  Rossen Stoyanchev

## Constructor Summary

Constructors
Constructor
Description
`ContentNegotiationConfigurer(ServletContext servletContext)`
Class constructor with `ServletContext`.

## Method Summary

Modifier and Type
Method
Description
`protected org.springframework.web.accept.ContentNegotiationManager`
`buildContentNegotiationManager()`
Build a `ContentNegotiationManager` based on this configurer's settings.
`ContentNegotiationConfigurer`
`defaultContentType(org.springframework.http.MediaType... defaultContentTypes)`
Set the default content type(s) to use when no content type is requested in order of priority.
`ContentNegotiationConfigurer`
`defaultContentTypeStrategy(org.springframework.web.accept.ContentNegotiationStrategy defaultStrategy)`
Set a custom `ContentNegotiationStrategy` to use to determine the content type to use when no content type is requested.
`ContentNegotiationConfigurer`
`favorParameter(boolean favorParameter)`
Whether a request parameter ("format" by default) should be used to determine the requested media type.
`ContentNegotiationConfigurer`
`favorPathExtension(boolean favorPathExtension)`
Deprecated.
as of 5.2.4.
`ContentNegotiationConfigurer`
`ignoreAcceptHeader(boolean ignoreAcceptHeader)`
Whether to disable checking the 'Accept' request header.
`ContentNegotiationConfigurer`
`ignoreUnknownPathExtensions(boolean ignore)`
Deprecated.
as of 5.2.4.
`ContentNegotiationConfigurer`
`mediaType(String extension, org.springframework.http.MediaType mediaType)`
Add a mapping from a key, extracted from a path extension or a query parameter, to a MediaType.
`ContentNegotiationConfigurer`
`mediaTypes(Map<String,org.springframework.http.MediaType> mediaTypes)`
An alternative to `mediaType(java.lang.String, org.springframework.http.MediaType)`.
`ContentNegotiationConfigurer`
`parameterName(String parameterName)`
Set the query parameter name to use when `favorParameter(boolean)` is on.
`ContentNegotiationConfigurer`
`replaceMediaTypes(Map<String,org.springframework.http.MediaType> mediaTypes)`
Similar to `mediaType(java.lang.String, org.springframework.http.MediaType)` but for replacing existing mappings.
`void`
`strategies(List<org.springframework.web.accept.ContentNegotiationStrategy> strategies)`
Set the exact list of strategies to use.
`ContentNegotiationConfigurer`
`useJaf(boolean useJaf)`
Deprecated.
as of 5.0, in favor of `useRegisteredExtensionsOnly(boolean)` which has reverse behavior
`ContentNegotiationConfigurer`
`useRegisteredExtensionsOnly(boolean useRegisteredExtensionsOnly)`
When `favorPathExtension` is set, this property determines whether to use only registered `MediaType` mappings to resolve a path extension to a specific MediaType.

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

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

## Constructor Details

### ContentNegotiationConfigurer

public ContentNegotiationConfigurer(@Nullable ServletContext servletContext)
Class constructor with `ServletContext`.

## Method Details

### strategies

public void strategies(@Nullable List<org.springframework.web.accept.ContentNegotiationStrategy> strategies)
Set the exact list of strategies to use.

Note: use of this method is mutually exclusive with use of all other setters in this class which customize a default, fixed set of strategies. See class level doc for more details.

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

### favorParameter

public ContentNegotiationConfigurer favorParameter(boolean favorParameter)
Whether a request parameter ("format" by default) should be used to determine the requested media type. For this option to work you must register `media type mappings`.

By default this is set to `false`.

See Also:
  - `parameterName(String)`

### parameterName

public ContentNegotiationConfigurer parameterName(String parameterName)
Set the query parameter name to use when `favorParameter(boolean)` is on.

The default parameter name is `"format"`.

### favorPathExtension

@Deprecated public ContentNegotiationConfigurer favorPathExtension(boolean favorPathExtension)
Deprecated.
as of 5.2.4. See deprecation note on `ContentNegotiationManagerFactoryBean.setFavorPathExtension(boolean)`.
Whether the path extension in the URL path should be used to determine the requested media type.

By default this is set to `false` in which case path extensions have no impact on content negotiation.

### mediaType

public ContentNegotiationConfigurer mediaType(String extension, org.springframework.http.MediaType mediaType)
Add a mapping from a key, extracted from a path extension or a query parameter, to a MediaType. This is required in order for the parameter strategy to work. Any extensions explicitly registered here are also treated as safe for the purpose of Reflected File Download attack detection (see Spring Framework reference documentation for more details on RFD attack protection).

The path extension strategy will also try to use `ServletContext.getMimeType(java.lang.String)` and `MediaTypeFactory` to resolve path extensions. To change this behavior see the `useRegisteredExtensionsOnly(boolean)` property.

Parameters:
  `extension` - the key to look up
  `mediaType` - the media type
See Also:
  - `mediaTypes(Map)`
  - `replaceMediaTypes(Map)`

### mediaTypes

public ContentNegotiationConfigurer mediaTypes(@Nullable Map<String,org.springframework.http.MediaType> mediaTypes)
An alternative to `mediaType(java.lang.String, org.springframework.http.MediaType)`.

See Also:
  - `mediaType(String, MediaType)`
  - `replaceMediaTypes(Map)`

### replaceMediaTypes

public ContentNegotiationConfigurer replaceMediaTypes(Map<String,org.springframework.http.MediaType> mediaTypes)
Similar to `mediaType(java.lang.String, org.springframework.http.MediaType)` but for replacing existing mappings.

See Also:
  - `mediaType(String, MediaType)`
  - `mediaTypes(Map)`

### ignoreUnknownPathExtensions

@Deprecated public ContentNegotiationConfigurer ignoreUnknownPathExtensions(boolean ignore)
Deprecated.
as of 5.2.4. See deprecation note on `ContentNegotiationManagerFactoryBean.setIgnoreUnknownPathExtensions(boolean)`.
Whether to ignore requests with path extension that cannot be resolved to any media type. Setting this to `false` will result in an `HttpMediaTypeNotAcceptableException` if there is no match.

By default this is set to `true`.

### useJaf

@Deprecated public ContentNegotiationConfigurer useJaf(boolean useJaf)
Deprecated.
as of 5.0, in favor of `useRegisteredExtensionsOnly(boolean)` which has reverse behavior
When `favorPathExtension(boolean)` is set, this property determines whether to allow use of JAF (Java Activation Framework) to resolve a path extension to a specific MediaType.

### useRegisteredExtensionsOnly

public ContentNegotiationConfigurer useRegisteredExtensionsOnly(boolean useRegisteredExtensionsOnly)
When `favorPathExtension` is set, this property determines whether to use only registered `MediaType` mappings to resolve a path extension to a specific MediaType.

By default this is not set in which case `PathExtensionContentNegotiationStrategy` will use defaults if available.

### ignoreAcceptHeader

public ContentNegotiationConfigurer ignoreAcceptHeader(boolean ignoreAcceptHeader)
Whether to disable checking the 'Accept' request header.

By default this value is set to `false`.

### defaultContentType

public ContentNegotiationConfigurer defaultContentType(org.springframework.http.MediaType... defaultContentTypes)
Set the default content type(s) to use when no content type is requested in order of priority.

If destinations are present that do not support any of the given media types, consider appending `MediaType.ALL` at the end.

By default this is not set.

See Also:
  - `defaultContentTypeStrategy(org.springframework.web.accept.ContentNegotiationStrategy)`

### defaultContentTypeStrategy

public ContentNegotiationConfigurer defaultContentTypeStrategy(org.springframework.web.accept.ContentNegotiationStrategy defaultStrategy)
Set a custom `ContentNegotiationStrategy` to use to determine the content type to use when no content type is requested.

By default this is not set.

Since:
  4.1.2
See Also:
  - `defaultContentType(org.springframework.http.MediaType...)`

### buildContentNegotiationManager

protected org.springframework.web.accept.ContentNegotiationManager buildContentNegotiationManager()
Build a `ContentNegotiationManager` based on this configurer's settings.

Since:
  4.3.12
See Also:
  - `ContentNegotiationManagerFactoryBean.getObject()`