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 | 
| favorPathExtension(boolean) | false (as of 5.3) | 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 SummaryConstructorsConstructorDescriptionContentNegotiationConfigurer(ServletContext servletContext) Class constructor withServletContext.
- 
Method SummaryModifier and TypeMethodDescriptionprotected ContentNegotiationManagerBuild aContentNegotiationManagerbased 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 customContentNegotiationStrategyto 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.favorPathExtension(boolean favorPathExtension) Deprecated.as of 5.2.4.ignoreAcceptHeader(boolean ignoreAcceptHeader) Whether to disable checking the 'Accept' request header.ignoreUnknownPathExtensions(boolean ignore) Deprecated.as of 5.2.4.Add a mapping from a key, extracted from a path extension or a query parameter, to a MediaType.mediaTypes(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.voidstrategies(List<ContentNegotiationStrategy> strategies) Set the exact list of strategies to use.useJaf(boolean useJaf) Deprecated.as of 5.0, in favor ofuseRegisteredExtensionsOnly(boolean)which has reverse behavioruseRegisteredExtensionsOnly(boolean useRegisteredExtensionsOnly) WhenfavorPathExtensionis set, this property determines whether to use only registeredMediaTypemappings to resolve a path extension to a specific MediaType.
- 
Constructor Details- 
ContentNegotiationConfigurerClass constructor withServletContext.
 
- 
- 
Method Details- 
strategiesSet 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
 
- 
favorParameterWhether 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:
 
- 
parameterNameSet the query parameter name to use whenfavorParameter(boolean)is on.The default parameter name is "format".
- 
favorPathExtensionDeprecated.as of 5.2.4. See deprecation note onContentNegotiationManagerFactoryBean.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 falsein which case path extensions have no impact on content negotiation.
- 
mediaTypeAdd 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)andMediaTypeFactoryto resolve path extensions. To change this behavior see theuseRegisteredExtensionsOnly(boolean)property.- Parameters:
- extension- the key to look up
- mediaType- the media type
- See Also:
 
- 
mediaTypesAn alternative tomediaType(java.lang.String, org.springframework.http.MediaType).
- 
replaceMediaTypesSimilar tomediaType(java.lang.String, org.springframework.http.MediaType)but for replacing existing mappings.- See Also:
 
- 
ignoreUnknownPathExtensionsDeprecated.as of 5.2.4. See deprecation note onContentNegotiationManagerFactoryBean.setIgnoreUnknownPathExtensions(boolean).Whether to ignore requests with path extension that cannot be resolved to any media type. Setting this tofalsewill result in anHttpMediaTypeNotAcceptableExceptionif there is no match.By default this is set to true.
- 
useJafDeprecated.as of 5.0, in favor ofuseRegisteredExtensionsOnly(boolean)which has reverse behaviorWhenfavorPathExtension(boolean)is set, this property determines whether to allow use of JAF (Java Activation Framework) to resolve a path extension to a specific MediaType.
- 
useRegisteredExtensionsOnlypublic ContentNegotiationConfigurer useRegisteredExtensionsOnly(boolean useRegisteredExtensionsOnly) WhenfavorPathExtensionis set, this property determines whether to use only registeredMediaTypemappings to resolve a path extension to a specific MediaType.By default this is not set in which case PathExtensionContentNegotiationStrategywill use defaults if available.
- 
ignoreAcceptHeaderWhether to disable checking the 'Accept' request header.By default this value is set to false.
- 
defaultContentTypeSet 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.ALLat the end.By default this is not set. 
- 
defaultContentTypeStrategypublic ContentNegotiationConfigurer defaultContentTypeStrategy(ContentNegotiationStrategy defaultStrategy) Set a customContentNegotiationStrategyto 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:
 
- 
buildContentNegotiationManagerBuild aContentNegotiationManagerbased on this configurer's settings.- Since:
- 4.3.12
- See Also:
 
 
-