@@ -5023,17 +5023,27 @@ public class WebConfig extends WebMvcConfigurerAdapter {
5023
5023
<section xml : id =" mvc-config-content-negotiation" >
5024
5024
<title >Configuring Content Negotiation</title >
5025
5025
5026
- <para >You can configure how Spring MVC determines requested media types for content negotiation purposes.
5027
- The available strategies are to check the request path extension, a
5028
- request parameter, the "Accept" header, and also to use a default content type.</para >
5029
-
5030
- <para >By default the path extension is checked first and the "Accept"
5031
- header is checked second. The path extension check recognizes ".json" if Jackson is available,
5032
- ".xml" if JAXB2 is available, and ".atom" and ".rss" if the Rome library is available.
5033
- It also uses the <classname >ServletContext</classname > and the <emphasis >Java Activation Framework</emphasis >
5034
- to perform lookups as a fallback option.</para >
5035
-
5036
- <para >An example of customizing content negotiation in Java:</para >
5026
+ <para >Staring with Spring Framework 3.2, you can configure how Spring MVC
5027
+ determines the requested media types from the client for request mapping
5028
+ as well as for content negotiation purposes. The available options are
5029
+ to check the file extension in the request URI, the "Accept" header,
5030
+ a request parameter, as well as to fall back on a default content type.
5031
+ By default, file extension in the request URI is checked first and
5032
+ the "Accept" header is checked next.</para >
5033
+
5034
+ <para >For file extensions in the request URI, the MVC Java config and
5035
+ the MVC namespace, automatically register extensions such as
5036
+ <filename >.json</filename >, <filename >.xml</filename >,
5037
+ <filename >.rss</filename >, and <filename >.atom</filename > if the
5038
+ corresponding dependencies such as Jackson, JAXB2, or Rome
5039
+ are present on the classpath. Additional extensions may be not need
5040
+ to be registered explicitly if they can be discovered via
5041
+ <classname >ServletContext.getMimeType(String)</classname > or the
5042
+ <emphasis >Java Activation Framework</emphasis >
5043
+ (see <classname >javax.activation.MimetypesFileTypeMap</classname >).</para >
5044
+
5045
+ <para >Below is an example of customizing content negotiation
5046
+ options through the MVC Java config:</para >
5037
5047
5038
5048
<programlisting language =" java" >@Configuration
5039
5049
@EnableWebMvc
@@ -5045,10 +5055,12 @@ public class WebConfig extends WebMvcConfigurerAdapter {
5045
5055
}
5046
5056
}</programlisting >
5047
5057
5048
- <para >In XML you'll need to use the <code >content-negotiation-manager</code >
5049
- property of <code >annotation-driven</code >:</para >
5058
+ <para >In the MVC namespace, the <code >< mvc:annotation-driven> </code > element
5059
+ has a <code >content-negotiation-manager</code > attribute, which expects a
5060
+ <classname >ContentNegotiationManager</classname > that in turn can be created
5061
+ with a <classname >ContentNegotiationManagerFactoryBean</classname >:</para >
5050
5062
5051
- <programlisting language =" xml" >< mvc:annotation-driven content-negotiation-manager="contentNegotiationManager" />
5063
+ <programlisting language =" xml" >< mvc:annotation-driven content-negotiation-manager="contentNegotiationManager" />
5052
5064
5053
5065
< bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
5054
5066
< property name="favorPathExtension" value="false" />
@@ -5061,6 +5073,30 @@ public class WebConfig extends WebMvcConfigurerAdapter {
5061
5073
< /property>
5062
5074
< /bean> </programlisting >
5063
5075
5076
+ <para >If not using the MVC Java config or the MVC namespace, you'll need
5077
+ to create an instance of <classname >ContentNegotiationManager</classname >
5078
+ and use it to configure
5079
+ <classname >RequestMappingHandlerMapping</classname > for request mapping
5080
+ purposes, and <classname >RequestMappingHandlerAdapter</classname > and
5081
+ <classname >ExceptionHandlerExceptionResolver</classname > for
5082
+ content negotiation purposes.</para >
5083
+
5084
+ <para >Note that <classname >ContentNegotiatingViewResolver</classname >
5085
+ now can also be configured with a <code >ContentNegotiatingViewResolver</code >,
5086
+ so you can use one instance throughout Spring MVC.</para >
5087
+
5088
+ <para >In more advanced cases, it may be useful to configure
5089
+ multiple <classname >ContentNegotiationManager</classname > instances
5090
+ that in turn may contain custom
5091
+ <classname >ContentNegotiationStrategy</classname > implementations.
5092
+ For example you could configure
5093
+ <classname >ExceptionHandlerExceptionResolver</classname > with
5094
+ a <classname >ContentNegotiationManager</classname > that always
5095
+ resolves the requested media type to <filename >"application/json"</filename >.
5096
+ Or you may want to plug a custom strategy that has some logic to select
5097
+ a default content type (e.g. either XML or JSON) if no content
5098
+ types were requested.</para >
5099
+
5064
5100
</section >
5065
5101
5066
5102
<section xml : id =" mvc-config-view-controller" >
0 commit comments