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:
| 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 MediaTypeFactory to resolve a path
extension to a MediaType.
| Constructor and Description |
|---|
ContentNegotiationConfigurer(ServletContext servletContext)
Class constructor with
ServletContext. |
| Modifier and Type | Method and Description |
|---|---|
ContentNegotiationConfigurer |
defaultContentType(org.springframework.http.MediaType... defaultContentTypes)
Set the default content type(s) to use when no content type is requested
in order of priority.
|
ContentNegotiationConfigurer |
defaultContentTypeStrategy(org.springframework.web.accept.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 org.springframework.web.accept.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,
org.springframework.http.MediaType mediaType)
Add a mapping from a key, extracted from a path extension or a query
parameter, to a MediaType.
|
ContentNegotiationConfigurer |
mediaTypes(Map<String,org.springframework.http.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,org.springframework.http.MediaType> mediaTypes)
Similar to
mediaType(java.lang.String, org.springframework.http.MediaType) but for replacing existing mappings. |
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(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, org.springframework.http.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 Map<String,org.springframework.http.MediaType> mediaTypes)
mediaType(java.lang.String, org.springframework.http.MediaType).public ContentNegotiationConfigurer replaceMediaTypes(Map<String,org.springframework.http.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 behavior.public 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(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(org.springframework.http.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(org.springframework.web.accept.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...)