Set all strategies in CNM factory bean

ContentNegotiationManagerFactoryBean now provides an option to
explicitly set the strategies to use vs customizing a fixed
list of default strategies.

Issue: SPR-11114
This commit is contained in:
Rossen Stoyanchev
2017-07-11 21:00:57 +02:00
parent befacf4a35
commit 134ceac58e
4 changed files with 119 additions and 59 deletions

View File

@@ -18,6 +18,7 @@ package org.springframework.web.servlet.config.annotation;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletContext;
@@ -34,9 +35,14 @@ import org.springframework.web.accept.PathExtensionContentNegotiationStrategy;
/**
* Creates a {@code ContentNegotiationManager} and configures it with
* one or more {@link ContentNegotiationStrategy} instances. The following shows
* the resulting strategy instances, the methods used to configured them, and
* whether enabled by default:
* one or more {@link ContentNegotiationStrategy} instances.
*
* <p>As of 5.0 you can set the exact strategies to use via
* {@link #strategies(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>
@@ -74,14 +80,12 @@ import org.springframework.web.accept.PathExtensionContentNegotiationStrategy;
* <p>The order in which strategies are configured is fixed. You can only turn
* them on or off.
*
* <p>For the path extension and parameter strategies you may explicitly add
* {@link #mediaType 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".
*
* <p>The path extension strategy will also use {@link ServletContext#getMimeType}
* and the {@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 #favorPathExtension}
* to {@literal false} or otherwise set the strategies to use explicitly via
* {@link #strategies(List)}.
*
* @author Rossen Stoyanchev
* @since 3.2
@@ -103,6 +107,18 @@ public class ContentNegotiationConfigurer {
}
/**
* 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 strategies(@Nullable List<ContentNegotiationStrategy> strategies) {
this.factory.setStrategies(strategies);
}
/**
* Whether the path extension in the URL path should be used to determine
* the requested media type.