spring-framework / org.springframework.web.accept / ContentNegotiationManagerFactoryBean

ContentNegotiationManagerFactoryBean

open class ContentNegotiationManagerFactoryBean : FactoryBean<ContentNegotiationManager>, ServletContextAware, InitializingBean

Factory to create a ContentNegotiationManager and configure it with one or more ContentNegotiationStrategy instances.

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

As an alternative you can also rely on the set of defaults described below which can be turned on or off or customized through the methods of this builder:

Property Setter Underlying Strategy Default Setting #setFavorPathExtension PathExtensionContentNegotiationStrategy On favorParameter ParameterContentNegotiationStrategy Off ignoreAcceptHeader HeaderContentNegotiationStrategy On defaultContentType FixedContentNegotiationStrategy Not set defaultContentTypeStrategy ContentNegotiationStrategy Not set 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 #setFavorPathExtension to false or otherwise set the strategies to use explicitly via #setStrategies(List).

Author
Rossen Stoyanchev

Author
Brian Clozel

Since
3.2

Constructors

<init>

ContentNegotiationManagerFactoryBean()

Factory to create a ContentNegotiationManager and configure it with one or more ContentNegotiationStrategy instances.

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

As an alternative you can also rely on the set of defaults described below which can be turned on or off or customized through the methods of this builder:

Property Setter Underlying Strategy Default Setting #setFavorPathExtension PathExtensionContentNegotiationStrategy On favorParameter ParameterContentNegotiationStrategy Off ignoreAcceptHeader HeaderContentNegotiationStrategy On defaultContentType FixedContentNegotiationStrategy Not set defaultContentTypeStrategy ContentNegotiationStrategy Not set 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 #setFavorPathExtension to false or otherwise set the strategies to use explicitly via #setStrategies(List).

Functions

addMediaType

open fun addMediaType(fileExtension: String, mediaType: MediaType): Unit

An alternative to #setMediaTypes for use in Java code.

addMediaTypes

open fun addMediaTypes(mediaTypes: MutableMap<String, MediaType>): Unit

An alternative to #setMediaTypes for use in Java code.

afterPropertiesSet

open fun afterPropertiesSet(): Unit

build

open fun build(): ContentNegotiationManager

Actually build the ContentNegotiationManager.

getObject

open fun getObject(): ContentNegotiationManager

getObjectType

open fun getObjectType(): Class<*>

isSingleton

open fun isSingleton(): Boolean

setDefaultContentType

open fun setDefaultContentType(contentType: MediaType): Unit

Set the default content type to use when no content type is requested.

By default this is not set.

setDefaultContentTypeStrategy

open fun setDefaultContentTypeStrategy(strategy: ContentNegotiationStrategy): Unit

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.

setDefaultContentTypes

open fun setDefaultContentTypes(contentTypes: MutableList<MediaType>): Unit

Set the default content types to use when no content type is requested.

By default this is not set.

setFavorParameter

open fun setFavorParameter(favorParameter: Boolean): Unit

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.

setFavorPathExtension

open fun setFavorPathExtension(favorPathExtension: Boolean): Unit

Whether the path extension in the URL path should be used to determine the requested media type.

By default this 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.

setIgnoreAcceptHeader

open fun setIgnoreAcceptHeader(ignoreAcceptHeader: Boolean): Unit

Whether to disable checking the 'Accept' request header.

By default this value is set to false.

setIgnoreUnknownPathExtensions

open fun setIgnoreUnknownPathExtensions(ignore: Boolean): Unit

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.

setMediaTypes

open fun setMediaTypes(mediaTypes: Properties): Unit

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 whitelisted 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 and org.springframework.http.MediaTypeFactory to resolve path extensions.

setParameterName

open fun setParameterName(parameterName: String): Unit

Set the query parameter name to use when #setFavorParameter is on.

The default parameter name is "format".

setServletContext

open fun setServletContext(servletContext: ServletContext): Unit

Invoked by Spring to inject the ServletContext.

setStrategies

open fun setStrategies(strategies: MutableList<ContentNegotiationStrategy>): Unit

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.

setUseJaf

open fun setUseJaf(useJaf: Boolean): Unit

setUseRegisteredExtensionsOnly

open fun setUseRegisteredExtensionsOnly(useRegisteredExtensionsOnly: Boolean): Unit

When favorPathExtension or #setFavorParameter(boolean) is set, this property determines whether to use only registered MediaType mappings or to allow dynamic resolution, e.g. via MediaTypeFactory.

By default this is not set in which case dynamic resolution is on.