Add options to configure content negotiation
The MVC Java config and the MVC namespace now support options to configure content negotiation. By default both support checking path extensions first and the "Accept" header second. For path extensions .json, .xml, .atom, and .rss are recognized out of the box if the Jackson, JAXB2, or Rome libraries are available. The ServletContext and the Java Activation Framework may be used as fallback options for path extension lookups. Issue: SPR-8420
This commit is contained in:
@@ -4478,6 +4478,54 @@ public class WebConfig extends WebMvcConfigurerAdapter {
|
||||
|
||||
</section>
|
||||
|
||||
<section id="mvc-config-content-negotiation">
|
||||
<title>Configuring Content Negotiation</title>
|
||||
|
||||
<para>You can configure how Spring MVC determines requested media types for content negotiation purposes.
|
||||
The available strategies are to check the request path extension, a
|
||||
request parameter, the "Accept" header, and also to use a default content type.</para>
|
||||
|
||||
<para>By default the path extension is checked first and the "Accept"
|
||||
header is checked second. The path extension check recognizes ".json" if Jackson is available,
|
||||
".xml" if JAXB2 is available, and ".atom" and ".rss" if the Rome library is available.
|
||||
It also uses the <classname>ServletContext</classname> and the <emphasis>Java Activation Framework</emphasis>
|
||||
to perform lookups as a fallback option.</para>
|
||||
|
||||
<para>An example of customizing content negotiation in Java:</para>
|
||||
|
||||
<programlisting language="java">@EnableWebMvc
|
||||
@Configuration
|
||||
public class WebConfig extends WebMvcConfigurerAdapter {
|
||||
|
||||
@Override
|
||||
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
|
||||
configurer.setFavorPathExtension(false).setFavorParameter(true);
|
||||
}
|
||||
}</programlisting>
|
||||
|
||||
<para>In XML you'll need to use the <code>content-negotiation-manager</code> property:</para>
|
||||
|
||||
<programlisting language="xml"><mvc:annotation-driven content-negotiation-manager="contentNegotiationManager" />
|
||||
|
||||
<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManager">
|
||||
<constructor-arg>
|
||||
<list>
|
||||
<ref bean="pathExtensionStrategy" />
|
||||
<bean id="headerStrategy" class="org.springframework.web.accept.HeaderContentNegotiationStrategy"/>
|
||||
</list>
|
||||
</constructor-arg>
|
||||
</bean>
|
||||
|
||||
<bean id="pathExtensionStrategy" class="org.springframework.web.accept.PathExtensionContentNegotiationStrategy">
|
||||
<constructor-arg>
|
||||
<map>
|
||||
<entry key="xml" value="application/rss+xml" />
|
||||
</map>
|
||||
</constructor-arg>
|
||||
</bean></programlisting>
|
||||
|
||||
</section>
|
||||
|
||||
<section id="mvc-config-view-controller">
|
||||
<title>Configuring View Controllers</title>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user