Update reference docs on content negotiation config
This commit is contained in:
@@ -5023,17 +5023,27 @@ public class WebConfig extends WebMvcConfigurerAdapter {
|
|||||||
<section xml:id="mvc-config-content-negotiation">
|
<section xml:id="mvc-config-content-negotiation">
|
||||||
<title>Configuring Content Negotiation</title>
|
<title>Configuring Content Negotiation</title>
|
||||||
|
|
||||||
<para>You can configure how Spring MVC determines requested media types for content negotiation purposes.
|
<para>Staring with Spring Framework 3.2, you can configure how Spring MVC
|
||||||
The available strategies are to check the request path extension, a
|
determines the requested media types from the client for request mapping
|
||||||
request parameter, the "Accept" header, and also to use a default content type.</para>
|
as well as for content negotiation purposes. The available options are
|
||||||
|
to check the file extension in the request URI, the "Accept" header,
|
||||||
|
a request parameter, as well as to fall back on a default content type.
|
||||||
|
By default, file extension in the request URI is checked first and
|
||||||
|
the "Accept" header is checked next.</para>
|
||||||
|
|
||||||
<para>By default the path extension is checked first and the "Accept"
|
<para>For file extensions in the request URI, the MVC Java config and
|
||||||
header is checked second. The path extension check recognizes ".json" if Jackson is available,
|
the MVC namespace, automatically register extensions such as
|
||||||
".xml" if JAXB2 is available, and ".atom" and ".rss" if the Rome library is available.
|
<filename>.json</filename>, <filename>.xml</filename>,
|
||||||
It also uses the <classname>ServletContext</classname> and the <emphasis>Java Activation Framework</emphasis>
|
<filename>.rss</filename>, and <filename>.atom</filename> if the
|
||||||
to perform lookups as a fallback option.</para>
|
corresponding dependencies such as Jackson, JAXB2, or Rome
|
||||||
|
are present on the classpath. Additional extensions may be not need
|
||||||
|
to be registered explicitly if they can be discovered via
|
||||||
|
<classname>ServletContext.getMimeType(String)</classname> or the
|
||||||
|
<emphasis>Java Activation Framework</emphasis>
|
||||||
|
(see <classname>javax.activation.MimetypesFileTypeMap</classname>).</para>
|
||||||
|
|
||||||
<para>An example of customizing content negotiation in Java:</para>
|
<para>Below is an example of customizing content negotiation
|
||||||
|
options through the MVC Java config:</para>
|
||||||
|
|
||||||
<programlisting language="java">@Configuration
|
<programlisting language="java">@Configuration
|
||||||
@EnableWebMvc
|
@EnableWebMvc
|
||||||
@@ -5045,10 +5055,12 @@ public class WebConfig extends WebMvcConfigurerAdapter {
|
|||||||
}
|
}
|
||||||
}</programlisting>
|
}</programlisting>
|
||||||
|
|
||||||
<para>In XML you'll need to use the <code>content-negotiation-manager</code>
|
<para>In the MVC namespace, the <code><mvc:annotation-driven></code> element
|
||||||
property of <code>annotation-driven</code>:</para>
|
has a <code>content-negotiation-manager</code> attribute, which expects a
|
||||||
|
<classname>ContentNegotiationManager</classname> that in turn can be created
|
||||||
|
with a <classname>ContentNegotiationManagerFactoryBean</classname>:</para>
|
||||||
|
|
||||||
<programlisting language="xml"><mvc:annotation-driven content-negotiation-manager="contentNegotiationManager" />
|
<programlisting language="xml"><mvc:annotation-driven content-negotiation-manager="contentNegotiationManager" />
|
||||||
|
|
||||||
<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
|
<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
|
||||||
<property name="favorPathExtension" value="false" />
|
<property name="favorPathExtension" value="false" />
|
||||||
@@ -5061,6 +5073,30 @@ public class WebConfig extends WebMvcConfigurerAdapter {
|
|||||||
</property>
|
</property>
|
||||||
</bean></programlisting>
|
</bean></programlisting>
|
||||||
|
|
||||||
|
<para>If not using the MVC Java config or the MVC namespace, you'll need
|
||||||
|
to create an instance of <classname>ContentNegotiationManager</classname>
|
||||||
|
and use it to configure
|
||||||
|
<classname>RequestMappingHandlerMapping</classname> for request mapping
|
||||||
|
purposes, and <classname>RequestMappingHandlerAdapter</classname> and
|
||||||
|
<classname>ExceptionHandlerExceptionResolver</classname> for
|
||||||
|
content negotiation purposes.</para>
|
||||||
|
|
||||||
|
<para>Note that <classname>ContentNegotiatingViewResolver</classname>
|
||||||
|
now can also be configured with a <code>ContentNegotiatingViewResolver</code>,
|
||||||
|
so you can use one instance throughout Spring MVC.</para>
|
||||||
|
|
||||||
|
<para>In more advanced cases, it may be useful to configure
|
||||||
|
multiple <classname>ContentNegotiationManager</classname> instances
|
||||||
|
that in turn may contain custom
|
||||||
|
<classname>ContentNegotiationStrategy</classname> implementations.
|
||||||
|
For example you could configure
|
||||||
|
<classname>ExceptionHandlerExceptionResolver</classname> with
|
||||||
|
a <classname>ContentNegotiationManager</classname> that always
|
||||||
|
resolves the requested media type to <filename>"application/json"</filename>.
|
||||||
|
Or you may want to plug a custom strategy that has some logic to select
|
||||||
|
a default content type (e.g. either XML or JSON) if no content
|
||||||
|
types were requested.</para>
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
<section xml:id="mvc-config-view-controller">
|
<section xml:id="mvc-config-view-controller">
|
||||||
|
|||||||
@@ -56,26 +56,20 @@
|
|||||||
|
|
||||||
<para>A <interfacename>ContentNeogtiationStrategy</interfacename> is now
|
<para>A <interfacename>ContentNeogtiationStrategy</interfacename> is now
|
||||||
available for resolving the requested media types from an incoming
|
available for resolving the requested media types from an incoming
|
||||||
request. The available implementations are based on path extension,
|
request. The available implementations are based on the file extension,
|
||||||
request parameter, 'Accept' header, and a fixed default content type.
|
query parameter, the 'Accept' header, or a fixed content type.
|
||||||
Equivalent options were previously available only in the
|
Equivalent options were previously available only in the
|
||||||
ContentNegotiatingViewResolver but are now available throughout.</para>
|
ContentNegotiatingViewResolver but are now available throughout.</para>
|
||||||
|
|
||||||
<para><classname>ContentNegotiationManager</classname> is the central
|
<para><classname>ContentNegotiationManager</classname> is the central
|
||||||
class to use when configuring content negotiation options. It accepts one
|
class to use when configuring content negotiation options.
|
||||||
or more ContentNeogtiationStrategy instances and delegates to them. It can
|
For more details see <xref linkend="mvc-config-content-negotiation" />.</para>
|
||||||
be plugged into <classname>RequestMappingHandlerMapping</classname>,
|
|
||||||
<classname>RequestMappingHandlerAdapter</classname>,
|
|
||||||
<classname>ExceptionHandlerExceptionResolver</classname>, and
|
|
||||||
<classname>ContentNegotiatingViewResolver</classname>. The MVC namespace
|
|
||||||
and the MVC JavaConfig provide convenient options to configure all
|
|
||||||
that.</para>
|
|
||||||
|
|
||||||
<para>The introduction of <classname>ContentNegotiationManger</classname>
|
<para>The introduction of <classname>ContentNegotiationManger</classname>
|
||||||
also enables smart suffix pattern matching for incoming requests. See
|
also enables selective suffix pattern matching for incoming requests.
|
||||||
|
For more details, see the Javadoc of
|
||||||
<link
|
<link
|
||||||
xl:href="https://github.com/SpringSource/spring-framework/commit/4fd7645">commit
|
xl:href="http://static.springsource.org/spring-framework/docs/3.2.0.BUILD-SNAPSHOT/api/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerMapping.html#setUseRegisteredSuffixPatternMatch(boolean)">RequestMappingHandlerMapping.setUseRegisteredSuffixPatternMatch</link>.</para>
|
||||||
message.</link></para>
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
<section xml:id="new-in-3.2-webmvc-controller-advice">
|
<section xml:id="new-in-3.2-webmvc-controller-advice">
|
||||||
|
|||||||
Reference in New Issue
Block a user