public class ContentNegotiationConfigurer extends Object
ContentNegotiationManager
and configures it with
one or more ContentNegotiationStrategy
instances.
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.
For the path extension and parameter strategies you may explicitly add
MediaType mappings
. Those will be used to resolve path
extensions and/or a query parameter value such as "json" to a concrete media
type such as "application/json".
The path extension strategy will also use ServletContext.getMimeType(java.lang.String)
and the Java Activation framework (JAF), if available, to resolve a path
extension to a MediaType. You may however suppress
the use
of JAF.
ContentNegotiationManagerFactoryBean
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 defaultContentType)
Set the default content type to use when no content type is requested.
|
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.
|
protected ContentNegotiationManager |
getContentNegotiationManager()
Deprecated.
as of 4.3.12, in favor of
buildContentNegotiationManager() |
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(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. |
ContentNegotiationConfigurer |
useJaf(boolean useJaf)
When
favorPathExtension(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(ServletContext servletContext)
ServletContext
.public 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(String extension, MediaType mediaType)
The path extension strategy will also try to use
ServletContext.getMimeType(java.lang.String)
and JAF (if present) to resolve path
extensions. To change this behavior see the useJaf(boolean)
property.
extension
- the key to look upmediaType
- the media typemediaTypes(Map)
,
replaceMediaTypes(Map)
public ContentNegotiationConfigurer mediaTypes(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)
public ContentNegotiationConfigurer ignoreUnknownPathExtensions(boolean ignore)
false
will result in an
HttpMediaTypeNotAcceptableException
if there is no match.
By default this is set to true
.
public ContentNegotiationConfigurer useJaf(boolean useJaf)
favorPathExtension(boolean)
is set, this property determines whether
to allow use of JAF (Java Activation Framework) to resolve a path
extension to a specific MediaType.
By default this is not set in which case
PathExtensionContentNegotiationStrategy
will use JAF if available.
public 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"
.
public ContentNegotiationConfigurer ignoreAcceptHeader(boolean ignoreAcceptHeader)
By default this value is set to false
.
public ContentNegotiationConfigurer defaultContentType(MediaType defaultContentType)
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()
@Deprecated protected ContentNegotiationManager getContentNegotiationManager() throws Exception
buildContentNegotiationManager()
Exception