@ -36,9 +36,14 @@ import org.springframework.web.context.ServletContextAware;
@@ -36,9 +36,14 @@ import org.springframework.web.context.ServletContextAware;
/ * *
* Factory to create a { @code ContentNegotiationManager } and configure it with
* one or more { @link ContentNegotiationStrategy } instances via simple setters .
* The following table shows setters , resulting strategy instances , and if in
* use by default :
* one or more { @link ContentNegotiationStrategy } instances .
*
* < p > As of 5 . 0 you can set the exact strategies to use via
* { @link # setStrategies ( List ) } .
*
* < p > 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 :
*
* < table >
* < tr >
@ -73,17 +78,12 @@ import org.springframework.web.context.ServletContextAware;
@@ -73,17 +78,12 @@ import org.springframework.web.context.ServletContextAware;
* < / tr >
* < / table >
*
* < p > The order in which strategies are configured is fixed . Setters may only
* turn individual strategies on or off . If you need a custom order for any
* reason simply instantiate { @code ContentNegotiationManager } directly .
*
* < p > For the path extension and parameter strategies you may explicitly add
* { @link # setMediaTypes MediaType mappings } . This will be used to resolve path
* extensions or a parameter value such as "json" to a media type such as
* "application/json" .
*
* < p > The path extension strategy will also use { @link ServletContext # getMimeType }
* and { @link MediaTypeFactory } to resolve a path extension to a MediaType .
* < strong > Note : < / strong > 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 { @link # setFavorPathExtension }
* to { @literal false } or otherwise set the strategies to use explicitly via
* { @link # setStrategies ( List ) } .
*
* @author Rossen Stoyanchev
* @since 3 . 2
@ -91,6 +91,10 @@ import org.springframework.web.context.ServletContextAware;
@@ -91,6 +91,10 @@ import org.springframework.web.context.ServletContextAware;
public class ContentNegotiationManagerFactoryBean
implements FactoryBean < ContentNegotiationManager > , ServletContextAware , InitializingBean {
@Nullable
private List < ContentNegotiationStrategy > strategies ;
private boolean favorPathExtension = true ;
private boolean favorParameter = false ;
@ -116,6 +120,18 @@ public class ContentNegotiationManagerFactoryBean
@@ -116,6 +120,18 @@ public class ContentNegotiationManagerFactoryBean
private ServletContext servletContext ;
/ * *
* Set the exact list of strategies to use .
* < p > < strong > Note : < / strong > 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 .
* @param strategies the strategies to use
* @since 5 . 0
* /
public void setStrategies ( @Nullable List < ContentNegotiationStrategy > strategies ) {
this . strategies = ( strategies ! = null ? new ArrayList < > ( strategies ) : null ) ;
}
/ * *
* Whether the path extension in the URL path should be used to determine
* the requested media type .
@ -280,41 +296,46 @@ public class ContentNegotiationManagerFactoryBean
@@ -280,41 +296,46 @@ public class ContentNegotiationManagerFactoryBean
public ContentNegotiationManager build ( ) {
List < ContentNegotiationStrategy > strategies = new ArrayList < > ( ) ;
if ( this . favorPathExtension ) {
PathExtensionContentNegotiationStrategy strategy ;
if ( this . servletContext ! = null & & ! useRegisteredExtensionsOnly ( ) ) {
strategy = new ServletPathExtensionContentNegotiationStrategy (
this . servletContext , this . mediaTypes ) ;
}
else {
strategy = new PathExtensionContentNegotiationStrategy ( this . mediaTypes ) ;
}
strategy . setIgnoreUnknownExtensions ( this . ignoreUnknownPathExtensions ) ;
if ( this . useRegisteredExtensionsOnly ! = null ) {
strategy . setUseRegisteredExtensionsOnly ( this . useRegisteredExtensionsOnly ) ;
}
strategies . add ( strategy ) ;
if ( this . strategies ! = null ) {
strategies . addAll ( this . strategies ) ;
}
if ( this . favorParameter ) {
ParameterContentNegotiationStrategy strategy =
new ParameterContentNegotiationStrategy ( this . mediaTypes ) ;
strategy . setParameterName ( this . parameterName ) ;
if ( this . useRegisteredExtensionsOnly ! = null ) {
strategy . setUseRegisteredExtensionsOnly ( this . useRegisteredExtensionsOnly ) ;
else {
if ( this . favorPathExtension ) {
PathExtensionContentNegotiationStrategy strategy ;
if ( this . servletContext ! = null & & ! useRegisteredExtensionsOnly ( ) ) {
strategy = new ServletPathExtensionContentNegotiationStrategy (
this . servletContext , this . mediaTypes ) ;
}
else {
strategy = new PathExtensionContentNegotiationStrategy ( this . mediaTypes ) ;
}
strategy . setIgnoreUnknownExtensions ( this . ignoreUnknownPathExtensions ) ;
if ( this . useRegisteredExtensionsOnly ! = null ) {
strategy . setUseRegisteredExtensionsOnly ( this . useRegisteredExtensionsOnly ) ;
}
strategies . add ( strategy ) ;
}
else {
strategy . setUseRegisteredExtensionsOnly ( true ) ; // backwards compatibility
if ( this . favorParameter ) {
ParameterContentNegotiationStrategy strategy =
new ParameterContentNegotiationStrategy ( this . mediaTypes ) ;
strategy . setParameterName ( this . parameterName ) ;
if ( this . useRegisteredExtensionsOnly ! = null ) {
strategy . setUseRegisteredExtensionsOnly ( this . useRegisteredExtensionsOnly ) ;
}
else {
strategy . setUseRegisteredExtensionsOnly ( true ) ; // backwards compatibility
}
strategies . add ( strategy ) ;
}
strategies . add ( strategy ) ;
}
if ( ! this . ignoreAcceptHeader ) {
strategies . add ( new HeaderContentNegotiationStrategy ( ) ) ;
}
if ( ! this . ignoreAcceptHeader ) {
strategies . add ( new HeaderContentNegotiationStrategy ( ) ) ;
}
if ( this . defaultNegotiationStrategy ! = null ) {
strategies . add ( this . defaultNegotiationStrategy ) ;
if ( this . defaultNegotiationStrategy ! = null ) {
strategies . add ( this . defaultNegotiationStrategy ) ;
}
}
this . contentNegotiationManager = new ContentNegotiationManager ( strategies ) ;