public class ContentNegotiationConfigurer extends 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(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) with a Map of registrations to add. |
ContentNegotiationConfigurer |
parameterName(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(Map<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(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)
with a Map of registrations to add.public ContentNegotiationConfigurer replaceMediaTypes(Map<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(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 Exception
ContentNegotiationManager
instanceException