public class ContentNegotiationConfigurer extends Object
ContentNegotiationManager
and configures it with
one or more ContentNegotiationStrategy
instances. The following shows
the resulting strategy instances, the methods used to configured them, and
whether enabled by default:
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.
Constructor and Description |
---|
ContentNegotiationConfigurer(ServletContext servletContext)
Class constructor with
ServletContext . |
Modifier and Type | Method and Description |
---|---|
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() |
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)
ServletContext.getMimeType(java.lang.String)
and JAF if it is present and is not
suppressed via useJaf(boolean)
.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.defaultContentType(org.springframework.http.MediaType)
protected ContentNegotiationManager getContentNegotiationManager() throws Exception
Exception