Class ContentNegotiationConfigurer
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 |
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)
.
- Since:
- 3.2
- Author:
- Rossen Stoyanchev
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected ContentNegotiationManager
Build aContentNegotiationManager
based on this configurer's settings.defaultContentType
(MediaType... defaultContentTypes) Set the default content type(s) to use when no content type is requested in order of priority.defaultContentTypeStrategy
(ContentNegotiationStrategy defaultStrategy) Set a customContentNegotiationStrategy
to use to determine the content type to use when no content type is requested.favorParameter
(boolean favorParameter) Whether a request parameter ("format" by default) should be used to determine the requested media type.ignoreAcceptHeader
(boolean ignoreAcceptHeader) Whether to disable checking the 'Accept' request header.Add a mapping from a key, extracted from a path extension or a query parameter, to a MediaType.mediaTypes
(@Nullable Map<String, MediaType> mediaTypes) An alternative tomediaType(java.lang.String, org.springframework.http.MediaType)
.parameterName
(String parameterName) Set the query parameter name to use whenfavorParameter(boolean)
is on.replaceMediaTypes
(Map<String, MediaType> mediaTypes) Similar tomediaType(java.lang.String, org.springframework.http.MediaType)
but for replacing existing mappings.void
strategies
(@Nullable List<ContentNegotiationStrategy> strategies) Set the exact list of strategies to use.useRegisteredExtensionsOnly
(boolean useRegisteredExtensionsOnly) WhenfavorParameter(boolean)
is set, this property determines whether to use only registeredMediaType
mappings to resolve a path extension to a specific MediaType.
-
Constructor Details
-
ContentNegotiationConfigurer
public ContentNegotiationConfigurer()
-
-
Method Details
-
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
Whether a request parameter ("format" by default) should be used to determine the requested media type. For this option to work you must registermedia type mappings
.By default this is set to
false
.- See Also:
-
parameterName
Set the query parameter name to use whenfavorParameter(boolean)
is on.The default parameter name is
"format"
. -
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)
andMediaTypeFactory
to resolve path extensions. To change this behavior see theuseRegisteredExtensionsOnly(boolean)
property.- Parameters:
extension
- the key to look upmediaType
- the media type- See Also:
-
mediaTypes
An alternative tomediaType(java.lang.String, org.springframework.http.MediaType)
. -
replaceMediaTypes
Similar tomediaType(java.lang.String, org.springframework.http.MediaType)
but for replacing existing mappings.- See Also:
-
useRegisteredExtensionsOnly
public ContentNegotiationConfigurer useRegisteredExtensionsOnly(boolean useRegisteredExtensionsOnly) WhenfavorParameter(boolean)
is set, this property determines whether to use only registeredMediaType
mappings to resolve a path extension to a specific MediaType.By default, this is not set in which case
ParameterContentNegotiationStrategy
will use defaults if available. -
ignoreAcceptHeader
Whether to disable checking the 'Accept' request header.By default this value is set to
false
. -
defaultContentType
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.
-
defaultContentTypeStrategy
public ContentNegotiationConfigurer defaultContentTypeStrategy(ContentNegotiationStrategy defaultStrategy) Set a customContentNegotiationStrategy
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:
-
buildContentNegotiationManager
Build aContentNegotiationManager
based on this configurer's settings.- Since:
- 4.3.12
- See Also:
-