Add ContentNegotiationManagerFactoryBean

The new FactoryBean facilitates the creation of a
ContentNegotiationManager in XML configuration.

Issue: SPR-8420
This commit is contained in:
Rossen Stoyanchev
2012-07-21 06:16:42 -04:00
parent 028e15faa3
commit 64d939bb16
6 changed files with 342 additions and 39 deletions

View File

@@ -35,11 +35,10 @@ import org.springframework.web.accept.PathExtensionContentNegotiationStrategy;
/**
* Helps with configuring a {@link ContentNegotiationManager}.
*
* <p>By default the extension of the request path extension is checked first and
* the {@code Accept} is checked second. The path extension check will perform a
* look up in the media types configured via {@link #setMediaTypes(Map)} and
* will also fall back to {@link ServletContext} and the Java Activation Framework
* (if present).
* <p>By default strategies for checking the extension of the request path and
* the {@code Accept} header are registered. The path extension check will perform
* lookups through the {@link ServletContext} and the Java Activation Framework
* (if present) unless {@linkplain #setMediaTypes(Map) media types} are configured.
*
* @author Rossen Stoyanchev
* @since 3.2
@@ -72,6 +71,16 @@ public class ContentNegotiationConfigurer {
return this;
}
/**
* Add mappings from file extensions to media types.
* <p>If this property is not set, the Java Action Framework, if available, may
* still be used in conjunction with {@link #setFavorPathExtension(boolean)}.
*/
public ContentNegotiationConfigurer addMediaType(String extension, MediaType mediaType) {
this.mediaTypes.put(extension, mediaType);
return this;
}
/**
* Add mappings from file extensions to media types.
* <p>If this property is not set, the Java Action Framework, if available, may

View File

@@ -452,7 +452,7 @@ public class MvcNamespaceTests {
@Test
public void testCustomContentNegotiationManager() throws Exception {
loadBeanDefinitions("mvc-config-content-negotiation-manager.xml", 14);
loadBeanDefinitions("mvc-config-content-negotiation-manager.xml", 12);
RequestMappingHandlerMapping mapping = appContext.getBean(RequestMappingHandlerMapping.class);
ContentNegotiationManager manager = mapping.getContentNegotiationManager();

View File

@@ -7,23 +7,12 @@
<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager" />
<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManager">
<constructor-arg>
<list>
<ref bean="pathExtensionStrategy" />
<ref bean="headerStrategy" />
</list>
</constructor-arg>
<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<property name="mediaTypes">
<value>
xml=application/rss+xml
</value>
</property>
</bean>
<bean id="pathExtensionStrategy" class="org.springframework.web.accept.PathExtensionContentNegotiationStrategy">
<constructor-arg>
<map>
<entry key="xml" value="application/rss+xml" />
</map>
</constructor-arg>
</bean>
<bean id="headerStrategy" class="org.springframework.web.accept.HeaderContentNegotiationStrategy" />
</beans>