public class ContentNegotiationConfigurer extends Object
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)
.
Constructor and Description |
---|
ContentNegotiationConfigurer(ServletContext servletContext)
Class constructor with
ServletContext . |
Modifier and Type | Method and Description |
---|---|
protected ContentNegotiationManager |
buildContentNegotiationManager()
Build a
ContentNegotiationManager based on this configurer's settings. |
ContentNegotiationConfigurer |
defaultContentType(MediaType... defaultContentTypes)
Set the default content type(s) to use when no content type is requested
in order of priority.
|
ContentNegotiationConfigurer |
defaultContentTypeStrategy(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. See deprecation note on
ContentNegotiationManagerFactoryBean.setFavorPathExtension(boolean) . |
ContentNegotiationConfigurer |
ignoreAcceptHeader(boolean ignoreAcceptHeader)
Whether to disable checking the 'Accept' request header.
|
ContentNegotiationConfigurer |
ignoreUnknownPathExtensions(boolean ignore)
Deprecated.
as of 5.2.4. See deprecation note on
ContentNegotiationManagerFactoryBean.setIgnoreUnknownPathExtensions(boolean) . |
ContentNegotiationConfigurer |
mediaType(String extension,
MediaType mediaType)
Add a mapping from a key, extracted from a path extension or a query
parameter, to a MediaType.
|
ContentNegotiationConfigurer |
mediaTypes(Map<String,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,MediaType> mediaTypes)
Similar to
mediaType(java.lang.String, org.springframework.http.MediaType) but for replacing existing mappings. |
void |
strategies(List<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. |
public ContentNegotiationConfigurer(@Nullable ServletContext servletContext)
ServletContext
.public void strategies(@Nullable List<ContentNegotiationStrategy> strategies)
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.
strategies
- the strategies to usepublic ContentNegotiationConfigurer favorParameter(boolean favorParameter)
media type mappings
.
By default this is set to false
.
parameterName(String)
public ContentNegotiationConfigurer parameterName(String parameterName)
favorParameter(boolean)
is on.
The default parameter name is "format"
.
@Deprecated public ContentNegotiationConfigurer favorPathExtension(boolean favorPathExtension)
ContentNegotiationManagerFactoryBean.setFavorPathExtension(boolean)
.By default this is set to false
in which case path extensions
have no impact on content negotiation.
public ContentNegotiationConfigurer mediaType(String extension, MediaType mediaType)
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.
extension
- the key to look upmediaType
- the media typemediaTypes(Map)
,
replaceMediaTypes(Map)
public ContentNegotiationConfigurer mediaTypes(@Nullable Map<String,MediaType> mediaTypes)
mediaType(java.lang.String, org.springframework.http.MediaType)
.public ContentNegotiationConfigurer replaceMediaTypes(Map<String,MediaType> mediaTypes)
mediaType(java.lang.String, org.springframework.http.MediaType)
but for replacing existing mappings.mediaType(String, MediaType)
,
mediaTypes(Map)
@Deprecated public ContentNegotiationConfigurer ignoreUnknownPathExtensions(boolean ignore)
ContentNegotiationManagerFactoryBean.setIgnoreUnknownPathExtensions(boolean)
.false
will result in an
HttpMediaTypeNotAcceptableException
if there is no match.
By default this is set to true
.
@Deprecated public ContentNegotiationConfigurer useJaf(boolean useJaf)
useRegisteredExtensionsOnly(boolean)
which has reverse behaviorfavorPathExtension(boolean)
is set, this property determines whether
to allow use of JAF (Java Activation Framework) to resolve a path
extension to a specific MediaType.public ContentNegotiationConfigurer useRegisteredExtensionsOnly(boolean useRegisteredExtensionsOnly)
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.
public ContentNegotiationConfigurer ignoreAcceptHeader(boolean ignoreAcceptHeader)
By default this value is set to false
.
public ContentNegotiationConfigurer defaultContentType(MediaType... defaultContentTypes)
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.
public ContentNegotiationConfigurer defaultContentTypeStrategy(ContentNegotiationStrategy defaultStrategy)
ContentNegotiationStrategy
to use to determine
the content type to use when no content type is requested.
By default this is not set.
defaultContentType(org.springframework.http.MediaType...)
protected ContentNegotiationManager buildContentNegotiationManager()
ContentNegotiationManager
based on this configurer's settings.ContentNegotiationManagerFactoryBean.getObject()