public class ContentNegotiationConfigurer
extends java.lang.Object
ContentNegotiationManager
.
By default strategies for checking the extension of the request path and
the Accept
header are registered. The path extension check will perform
lookups through the ServletContext
and the Java Activation Framework
(if present) unless media types are configured.
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 was requested.
|
ContentNegotiationConfigurer |
defaultContentTypeStrategy(ContentNegotiationStrategy defaultStrategy)
Configure a custom
ContentNegotiationStrategy to use to determine
the default content type to use when no content type was requested. |
ContentNegotiationConfigurer |
favorParameter(boolean favorParameter)
Indicate whether a request parameter should be used to determine the
requested media type with the 2nd highest priority, i.e.
|
ContentNegotiationConfigurer |
favorPathExtension(boolean favorPathExtension)
Indicate whether the extension of the request path should be used to determine
the requested media type with the highest priority.
|
protected ContentNegotiationManager |
getContentNegotiationManager()
Return the configured
ContentNegotiationManager instance |
ContentNegotiationConfigurer |
ignoreAcceptHeader(boolean ignoreAcceptHeader)
Indicate whether the HTTP
Accept header should be ignored altogether. |
ContentNegotiationConfigurer |
ignoreUnknownPathExtensions(boolean ignore)
Whether to ignore requests that have a file extension that does not match
any mapped media types.
|
ContentNegotiationConfigurer |
mediaType(java.lang.String extension,
MediaType mediaType)
Add mappings from file extensions to media types.
|
ContentNegotiationConfigurer |
mediaTypes(java.util.Map<java.lang.String,MediaType> mediaTypes)
Add mappings from file extensions to media types.
|
ContentNegotiationConfigurer |
parameterName(java.lang.String parameterName)
Set the parameter name that can be used to determine the requested media type
if the
favorParameter(boolean) property is true . |
ContentNegotiationConfigurer |
replaceMediaTypes(java.util.Map<java.lang.String,MediaType> mediaTypes)
Add mappings from file extensions to media types replacing any previous mappings.
|
ContentNegotiationConfigurer |
useJaf(boolean useJaf)
Indicate whether to use the Java Activation Framework as a fallback option
to map from file extensions to media types.
|
public ContentNegotiationConfigurer(ServletContext servletContext)
ServletContext
.public ContentNegotiationConfigurer favorPathExtension(boolean favorPathExtension)
By default this value 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)
If this property is not set, the Java Action Framework, if available, may
still be used in conjunction with favorPathExtension(boolean)
.
public ContentNegotiationConfigurer mediaTypes(java.util.Map<java.lang.String,MediaType> mediaTypes)
If this property is not set, the Java Action Framework, if available, may
still be used in conjunction with favorPathExtension(boolean)
.
public ContentNegotiationConfigurer replaceMediaTypes(java.util.Map<java.lang.String,MediaType> mediaTypes)
If this property is not set, the Java Action Framework, if available, may
still be used in conjunction with favorPathExtension(boolean)
.
public ContentNegotiationConfigurer ignoreUnknownPathExtensions(boolean ignore)
false
will result in a
HttpMediaTypeNotAcceptableException
when there is no match.
By default this is set to true
.
public ContentNegotiationConfigurer useJaf(boolean useJaf)
favorPathExtension(boolean)
is set to true
.
The default value is true
.
parameterName(java.lang.String)
,
mediaTypes(Map)
public ContentNegotiationConfigurer favorParameter(boolean favorParameter)
Accept
header.
The default value is false
. If set to to true
, a request
for /hotels?format=pdf
will be interpreted as a request for
"application/pdf"
regardless of the Accept
header.
To use this option effectively you must also configure the MediaType
type mappings via mediaTypes(Map)
.
parameterName(String)
public ContentNegotiationConfigurer parameterName(java.lang.String parameterName)
favorParameter(boolean)
property is true
.
The default parameter name is "format"
.
public ContentNegotiationConfigurer ignoreAcceptHeader(boolean ignoreAcceptHeader)
Accept
header should be ignored altogether.
If set the Accept
header is checked at the
3rd highest priority, i.e. after the request path extension and
possibly a request parameter if configured.
By default this value is set to false
.
public ContentNegotiationConfigurer defaultContentType(MediaType defaultContentType)
Note that internally this method creates and adds a
FixedContentNegotiationStrategy
. Alternatively you can also provide a
custom strategy via defaultContentTypeStrategy(org.springframework.web.accept.ContentNegotiationStrategy)
.
public ContentNegotiationConfigurer defaultContentTypeStrategy(ContentNegotiationStrategy defaultStrategy)
ContentNegotiationStrategy
to use to determine
the default content type to use when no content type was requested.
However also consider using defaultContentType(org.springframework.http.MediaType)
which provides
a simpler alternative to doing the same.
protected ContentNegotiationManager getContentNegotiationManager() throws java.lang.Exception
ContentNegotiationManager
instancejava.lang.Exception