类 ContentNegotiationConfigurer
- java.lang.Object
- org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer
public class ContentNegotiationConfigurer extends Object
Creates aContentNegotiationManagerand configures it with one or moreContentNegotiationStrategyinstances.This factory offers properties that in turn result in configuring the underlying strategies. The table below shows the property names, their default settings, as well as the strategies that they help to configure:
Property Setter Default Value Underlying Strategy Enabled Or Not favorPathExtension(boolean)true PathExtensionContentNegotiationStrategyEnabled favorParameter(boolean)false ParameterContentNegotiationStrategyOff ignoreAcceptHeader(boolean)false HeaderContentNegotiationStrategyEnabled defaultContentType(org.springframework.http.MediaType...)null FixedContentNegotiationStrategyOff defaultContentTypeStrategy(org.springframework.web.accept.ContentNegotiationStrategy)null ContentNegotiationStrategyOff As of 5.0 you can set the exact strategies to use via
strategies(List).Note: if you must use URL-based content type resolution, the use of a query parameter is simpler and preferable to the use of a path extension since the latter can cause issues with URI variables, path parameters, and URI decoding. Consider setting
favorPathExtension(boolean)to false or otherwise set the strategies to use explicitly viastrategies(List).- 从以下版本开始:
- 3.2
- 作者:
- Rossen Stoyanchev
构造器概要
构造器 构造器 说明 ContentNegotiationConfigurer(ServletContext servletContext)Class constructor withServletContext.
方法概要
所有方法 实例方法 具体方法 已过时的方法 修饰符和类型 方法 说明 protected ContentNegotiationManagerbuildContentNegotiationManager()Build aContentNegotiationManagerbased on this configurer's settings.ContentNegotiationConfigurerdefaultContentType(MediaType... defaultContentTypes)Set the default content type(s) to use when no content type is requested in order of priority.ContentNegotiationConfigurerdefaultContentTypeStrategy(ContentNegotiationStrategy defaultStrategy)Set a customContentNegotiationStrategyto use to determine the content type to use when no content type is requested.ContentNegotiationConfigurerfavorParameter(boolean favorParameter)Whether a request parameter ("format" by default) should be used to determine the requested media type.ContentNegotiationConfigurerfavorPathExtension(boolean favorPathExtension)已过时。as of 5.2.4.ContentNegotiationConfigurerignoreAcceptHeader(boolean ignoreAcceptHeader)Whether to disable checking the 'Accept' request header.ContentNegotiationConfigurerignoreUnknownPathExtensions(boolean ignore)已过时。as of 5.2.4.ContentNegotiationConfigurermediaType(String extension, MediaType mediaType)Add a mapping from a key, extracted from a path extension or a query parameter, to a MediaType.ContentNegotiationConfigurermediaTypes(Map<String,MediaType> mediaTypes)An alternative tomediaType(java.lang.String, org.springframework.http.MediaType).ContentNegotiationConfigurerparameterName(String parameterName)Set the query parameter name to use whenfavorParameter(boolean)is on.ContentNegotiationConfigurerreplaceMediaTypes(Map<String,MediaType> mediaTypes)Similar tomediaType(java.lang.String, org.springframework.http.MediaType)but for replacing existing mappings.voidstrategies(List<ContentNegotiationStrategy> strategies)Set the exact list of strategies to use.ContentNegotiationConfigureruseJaf(boolean useJaf)已过时。as of 5.0, in favor ofuseRegisteredExtensionsOnly(boolean)which has reverse behaviorContentNegotiationConfigureruseRegisteredExtensionsOnly(boolean useRegisteredExtensionsOnly)WhenfavorPathExtensionis set, this property determines whether to use only registeredMediaTypemappings to resolve a path extension to a specific MediaType.
构造器详细资料
ContentNegotiationConfigurer
public ContentNegotiationConfigurer(@Nullable ServletContext servletContext)
Class constructor withServletContext.
方法详细资料
strategies
public void strategies(@Nullable List<ContentNegotiationStrategy> strategies)
Set the exact list of strategies to use.Note: use of this method is mutually exclusive with use of all other setters in this class which customize a default, fixed set of strategies. See class level doc for more details.
- 参数:
strategies- the strategies to use- 从以下版本开始:
- 5.0
favorPathExtension
@Deprecated public ContentNegotiationConfigurer favorPathExtension(boolean favorPathExtension)
已过时。as of 5.2.4. See class-level note inContentNegotiationManagerFactoryBeanon the deprecation of path extension config options. As there is no replacement for this method, for the time being it's necessary to continue using it in order to set it tofalse. In 5.3 whenfalsebecomes the default, use of this property will no longer be necessary.Whether the path extension in the URL path should be used to determine the requested media type.By default this is set to
truein which case a request for/hotels.pdfwill be interpreted as a request for"application/pdf"regardless of the 'Accept' header.
mediaType
public ContentNegotiationConfigurer mediaType(String extension, MediaType mediaType)
Add a mapping from a key, extracted from a path extension or a query parameter, to a MediaType. This is required in order for the parameter strategy to work. Any extensions explicitly registered here are also treated as safe for the purpose of Reflected File Download attack detection (see Spring Framework reference documentation for more details on RFD attack protection).The path extension strategy will also try to use
ServletContext.getMimeType(java.lang.String)andMediaTypeFactoryto resolve path extensions. To change this behavior see theuseRegisteredExtensionsOnly(boolean)property.- 参数:
extension- the key to look upmediaType- the media type- 另请参阅:
mediaTypes(Map),replaceMediaTypes(Map)
mediaTypes
public ContentNegotiationConfigurer mediaTypes(@Nullable Map<String,MediaType> mediaTypes)
An alternative tomediaType(java.lang.String, org.springframework.http.MediaType).
replaceMediaTypes
public ContentNegotiationConfigurer replaceMediaTypes(Map<String,MediaType> mediaTypes)
Similar tomediaType(java.lang.String, org.springframework.http.MediaType)but for replacing existing mappings.
ignoreUnknownPathExtensions
@Deprecated public ContentNegotiationConfigurer ignoreUnknownPathExtensions(boolean ignore)
已过时。as of 5.2.4. See class-level note inContentNegotiationManagerFactoryBeanon the deprecation of path extension config options.Whether to ignore requests with path extension that cannot be resolved to any media type. Setting this tofalsewill result in anHttpMediaTypeNotAcceptableExceptionif there is no match.By default this is set to
true.
useJaf
@Deprecated public ContentNegotiationConfigurer useJaf(boolean useJaf)
已过时。as of 5.0, in favor ofuseRegisteredExtensionsOnly(boolean)which has reverse behaviorWhenfavorPathExtension(boolean)is set, this property determines whether to allow use of JAF (Java Activation Framework) to resolve a path extension to a specific MediaType.
useRegisteredExtensionsOnly
public ContentNegotiationConfigurer useRegisteredExtensionsOnly(boolean useRegisteredExtensionsOnly)
WhenfavorPathExtensionis set, this property determines whether to use only registeredMediaTypemappings to resolve a path extension to a specific MediaType.By default this is not set in which case
PathExtensionContentNegotiationStrategywill use defaults if available.
favorParameter
public ContentNegotiationConfigurer favorParameter(boolean favorParameter)
Whether a request parameter ("format" by default) should be used to determine the requested media type. For this option to work you must registermedia type mappings.By default this is set to
false.
parameterName
public ContentNegotiationConfigurer parameterName(String parameterName)
Set the query parameter name to use whenfavorParameter(boolean)is on.The default parameter name is
"format".
ignoreAcceptHeader
public ContentNegotiationConfigurer ignoreAcceptHeader(boolean ignoreAcceptHeader)
Whether to disable checking the 'Accept' request header.By default this value is set to
false.
defaultContentType
public ContentNegotiationConfigurer defaultContentType(MediaType... defaultContentTypes)
Set the default content type(s) to use when no content type is requested in order of priority.If destinations are present that do not support any of the given media types, consider appending
MediaType.ALLat the end.By default this is not set.
defaultContentTypeStrategy
public ContentNegotiationConfigurer defaultContentTypeStrategy(ContentNegotiationStrategy defaultStrategy)
Set a customContentNegotiationStrategyto use to determine the content type to use when no content type is requested.By default this is not set.
- 从以下版本开始:
- 4.1.2
- 另请参阅:
defaultContentType(org.springframework.http.MediaType...)
buildContentNegotiationManager
protected ContentNegotiationManager buildContentNegotiationManager()
Build aContentNegotiationManagerbased on this configurer's settings.- 从以下版本开始:
- 4.3.12
- 另请参阅:
ContentNegotiationManagerFactoryBean.getObject()