org.springframework.web.servlet.config.annotation

## Class ContentNegotiationConfigurer

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

    public class ContentNegotiationConfigurer
    extends Object

Helps with configuring a `ContentNegotiationManager`.

By default strategies for checking the extension of the request path and the `Accept` header are registered. The path extension check will perform lookups through the `ServletContext` and the Java Activation Framework (if present) unless media types are configured.

Since:
  3.2
Author:
  Rossen Stoyanchev

### Constructor Summary

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

### Method Summary

Methods
`ContentNegotiationConfigurer` `defaultContentType(MediaType defaultContentType)` Set the default content type.
`ContentNegotiationConfigurer` `favorParameter(boolean favorParameter)` Indicate whether a request parameter should be used to determine the requested media type with the 2nd highest priority, i.e.
`ContentNegotiationConfigurer` `favorPathExtension(boolean favorPathExtension)` Indicate whether the extension of the request path should be used to determine the requested media type with the highest priority.
`protected ContentNegotiationManager` `getContentNegotiationManager()` Return the configured `ContentNegotiationManager` instance
`ContentNegotiationConfigurer` `ignoreAcceptHeader(boolean ignoreAcceptHeader)` Indicate whether the HTTP `Accept` header should be ignored altogether.
`ContentNegotiationConfigurer` `mediaType(String extension, MediaType mediaType)` Add mappings from file extensions to media types.
`ContentNegotiationConfigurer` `mediaTypes(Map<String,MediaType> mediaTypes)` Add mappings from file extensions to media types.
`ContentNegotiationConfigurer` `parameterName(String parameterName)` Set the parameter name that can be used to determine the requested media type if the `favorParameter(boolean)` property is `true`.
`ContentNegotiationConfigurer` `replaceMediaTypes(Map<String,MediaType> mediaTypes)` Add mappings from file extensions to media types replacing any previous mappings.
`ContentNegotiationConfigurer` `useJaf(boolean useJaf)` Indicate whether to use the Java Activation Framework as a fallback option to map from file extensions to media types.

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

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

### Constructor Detail

#### ContentNegotiationConfigurer

    public ContentNegotiationConfigurer(ServletContext servletContext)

Class constructor with `ServletContext`.

### Method Detail

#### favorPathExtension

    public ContentNegotiationConfigurer favorPathExtension(boolean favorPathExtension)

Indicate whether the extension of the request path should be used to determine the requested media type with the highest priority.

By default this value is set to `true` in which case a request for `/hotels.pdf` will be interpreted as a request for `"application/pdf"` regardless of the `Accept` header.

#### mediaType

    public ContentNegotiationConfigurer mediaType(String extension,
                                         MediaType mediaType)

Add mappings from file extensions to media types.

If this property is not set, the Java Action Framework, if available, may still be used in conjunction with `favorPathExtension(boolean)`.

#### mediaTypes

    public ContentNegotiationConfigurer mediaTypes(Map<String,MediaType> mediaTypes)

Add mappings from file extensions to media types.

If this property is not set, the Java Action Framework, if available, may still be used in conjunction with `favorPathExtension(boolean)`.

#### replaceMediaTypes

    public ContentNegotiationConfigurer replaceMediaTypes(Map<String,MediaType> mediaTypes)

Add mappings from file extensions to media types replacing any previous mappings.

If this property is not set, the Java Action Framework, if available, may still be used in conjunction with `favorPathExtension(boolean)`.

#### useJaf

    public ContentNegotiationConfigurer useJaf(boolean useJaf)

Indicate whether to use the Java Activation Framework as a fallback option to map from file extensions to media types. This is used only when `favorPathExtension(boolean)` is set to `true`.

The default value is `true`.

See Also:
  `parameterName(java.lang.String)`, `mediaTypes(Map)`

#### favorParameter

    public ContentNegotiationConfigurer favorParameter(boolean favorParameter)

Indicate whether a request parameter should be used to determine the requested media type with the 2nd highest priority, i.e. after path extensions but before the `Accept` header.

The default value is `false`. If set to to `true`, a request for `/hotels?format=pdf` will be interpreted as a request for `"application/pdf"` regardless of the `Accept` header.

To use this option effectively you must also configure the MediaType type mappings via `mediaTypes(Map)`.

See Also:
  `parameterName(String)`

#### parameterName

    public ContentNegotiationConfigurer parameterName(String parameterName)

Set the parameter name that can be used to determine the requested media type if the `favorParameter(boolean)` property is `true`.

The default parameter name is `"format"`.

#### ignoreAcceptHeader

    public ContentNegotiationConfigurer ignoreAcceptHeader(boolean ignoreAcceptHeader)

Indicate whether the HTTP `Accept` header should be ignored altogether. If set the `Accept` header is checked at the 3rd highest priority, i.e. after the request path extension and possibly a request parameter if configured.

By default this value is set to `false`.

#### defaultContentType

    public ContentNegotiationConfigurer defaultContentType(MediaType defaultContentType)

Set the default content type.

This content type will be used when neither the request path extension, nor a request parameter, nor the `Accept` header could help determine the requested content type.

#### getContentNegotiationManager

    protected ContentNegotiationManager getContentNegotiationManager()
                                                              throws Exception

Return the configured `ContentNegotiationManager` instance

Throws:
  `Exception`