类 ContentNegotiationConfigurer
- java.lang.Object
- org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer
public class ContentNegotiationConfigurer extends Object
Creates aContentNegotiationManagerand configures it with one or moreContentNegotiationStrategyinstances.As an alternative you can also rely on the set of defaults described below which can be turned on or off or customized through the methods of this builder:
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 howeversuppressthe use of JAF.- 从以下版本开始:
- 3.2
- 作者:
- Rossen Stoyanchev, Brian Clozel, Juergen Hoeller
- 另请参阅:
ContentNegotiationManagerFactoryBean
构造器概要
构造器 构造器 说明 ContentNegotiationConfigurer(ServletContext servletContext)Class constructor withServletContext.
方法概要
所有方法 实例方法 具体方法 已过时的方法 修饰符和类型 方法 说明 protected ContentNegotiationManagerbuildContentNegotiationManager()Build aContentNegotiationManagerbased on this configurer's settings.ContentNegotiationConfigurerdefaultContentType(MediaType defaultContentType)Set the default content type to use when no content type is requested.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)Whether the path extension in the URL path should be used to determine the requested media type.protected ContentNegotiationManagergetContentNegotiationManager()已过时。as of 4.3.12, in favor ofbuildContentNegotiationManager()ContentNegotiationConfigurerignoreAcceptHeader(boolean ignoreAcceptHeader)Whether to disable checking the 'Accept' request header.ContentNegotiationConfigurerignoreUnknownPathExtensions(boolean ignore)Whether to ignore requests with path extension that cannot be resolved to any media type.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.ContentNegotiationConfigureruseJaf(boolean useJaf)WhenfavorPathExtension(boolean)is set, this property determines whether to allow use of JAF (Java Activation Framework) to resolve a path extension to a specific MediaType.
构造器详细资料
ContentNegotiationConfigurer
public ContentNegotiationConfigurer(ServletContext servletContext)
Class constructor withServletContext.
方法详细资料
favorPathExtension
public ContentNegotiationConfigurer favorPathExtension(boolean favorPathExtension)
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 whitelisted 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)and JAF (if present) to resolve path extensions. To change this behavior see theuseJaf(boolean)property.- 参数:
extension- the key to look upmediaType- the media type- 另请参阅:
mediaTypes(Map),replaceMediaTypes(Map)
mediaTypes
public ContentNegotiationConfigurer mediaTypes(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
public ContentNegotiationConfigurer ignoreUnknownPathExtensions(boolean ignore)
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
public ContentNegotiationConfigurer useJaf(boolean useJaf)
WhenfavorPathExtension(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
PathExtensionContentNegotiationStrategywill use JAF 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 defaultContentType)
Set the default content type to use when no content type is requested.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()
getContentNegotiationManager
@Deprecated protected ContentNegotiationManager getContentNegotiationManager() throws Exception
已过时。as of 4.3.12, in favor ofbuildContentNegotiationManager()- 抛出:
Exception