public class ContentNegotiationConfigurer
extends java.lang.Object
ContentNegotiationManager
and configures it with
one or more ContentNegotiationStrategy
instances.
As of 5.0 you can set the exact strategies to use via
strategies(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:
Configurer Property | Underlying Strategy | Default Setting |
---|---|---|
favorPathExtension(boolean) |
Path Extension strategy |
On |
favorParameter(boolean) |
Parameter strategy |
Off |
ignoreAcceptHeader(boolean) |
Header strategy |
On |
defaultContentType(org.springframework.http.MediaType...) |
Fixed content strategy |
Not set |
defaultContentTypeStrategy(org.springframework.web.accept.ContentNegotiationStrategy) |
ContentNegotiationStrategy |
Not set |
The order in which strategies are configured is fixed. You can only turn
them on or off.
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)
Whether the path extension in the URL path should be used to determine
the requested media type.
|
ContentNegotiationConfigurer |
ignoreAcceptHeader(boolean ignoreAcceptHeader)
Whether to disable checking the 'Accept' request header.
|
ContentNegotiationConfigurer |
ignoreUnknownPathExtensions(boolean ignore)
Whether to ignore requests with path extension that cannot be resolved
to any media type.
|
ContentNegotiationConfigurer |
mediaType(java.lang.String extension,
MediaType mediaType)
Add a mapping from a key, extracted from a path extension or a query
parameter, to a MediaType.
|
ContentNegotiationConfigurer |
mediaTypes(java.util.Map<java.lang.String,MediaType> mediaTypes)
An alternative to
mediaType(java.lang.String, org.springframework.http.MediaType) . |
ContentNegotiationConfigurer |
parameterName(java.lang.String parameterName)
Set the query parameter name to use when
favorParameter(boolean) is on. |
ContentNegotiationConfigurer |
replaceMediaTypes(java.util.Map<java.lang.String,MediaType> mediaTypes)
Similar to
mediaType(java.lang.String, org.springframework.http.MediaType) but for replacing existing mappings. |
void |
strategies(java.util.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 java.util.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 favorPathExtension(boolean favorPathExtension)
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.
public ContentNegotiationConfigurer mediaType(java.lang.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 java.util.Map<java.lang.String,MediaType> mediaTypes)
mediaType(java.lang.String, org.springframework.http.MediaType)
.public ContentNegotiationConfigurer replaceMediaTypes(java.util.Map<java.lang.String,MediaType> mediaTypes)
mediaType(java.lang.String, org.springframework.http.MediaType)
but for replacing existing mappings.mediaType(String, MediaType)
,
mediaTypes(Map)
public ContentNegotiationConfigurer ignoreUnknownPathExtensions(boolean ignore)
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 behaviorpublic 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 favorParameter(boolean favorParameter)
media type mappings
.
By default this is set to false
.
parameterName(String)
public ContentNegotiationConfigurer parameterName(java.lang.String parameterName)
favorParameter(boolean)
is on.
The default parameter name is "format"
.
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()