從Spring 3.2開始,您還可以設置RequestMappingHandlerMapping bean的useRegisteredSuffixPatternMatch屬性,以便激活後綴模式識別,但僅限於註冊擴展。
在這裏,你只定義了JSON和XML擴展:
<bean id="handlerMapping"
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
<property name="contentNegotiationManager" ref="contentNegotiationManager"/>
<property name="useRegisteredSuffixPatternMatch" value="true"/>
</bean>
<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<property name="favorPathExtension" value="false"/>
<property name="favorParameter" value="true"/>
<property name="mediaTypes">
<value>
json=application/json
xml=application/xml
</value>
</property>
</bean>
需要注意的是MVC:註解驅動現在接受contentNegotiation選項提供自定義的bean,但RequestMappingHandlerMapping的財產已被更改爲true(默認假)(參見https://jira.springsource.org/browse/SPR-7632)。
因此,您仍然必須重寫所有mvc:註釋驅動的配置。我打開了Spring的門票以請求自定義的RequestMappingHandlerMapping:https://jira.springsource.org/browse/SPR-11253。請投票,如果你有興趣。
重寫時,要小心也考慮自定義執行管理覆蓋。否則,您所有的自定義異常映射都將失敗。你將有一個列表豆重用messageCoverters:
<util:list id="messageConverters">
<bean class="your.custom.message.converter.IfAny"></bean>
<bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"></bean>
<bean class="org.springframework.http.converter.StringHttpMessageConverter"></bean>
<bean class="org.springframework.http.converter.ResourceHttpMessageConverter"></bean>
<bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter"></bean>
<bean class="org.springframework.http.converter.xml.XmlAwareFormHttpMessageConverter"></bean>
<bean class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter"></bean>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>
</util:list>
<bean name="exceptionHandlerExceptionResolver"
class="org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver">
<property name="order" value="0"/>
<property name="messageConverters" ref="messageConverters"/>
</bean>
<bean name="handlerAdapter"
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="webBindingInitializer">
<bean class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
<property name="validator">
<bean class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>
</property>
</bean>
</property>
<property name="messageConverters" ref="messageConverters"/>
</bean>
<bean id="handlerMapping"
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
<property name="contentNegotiationManager" ref="contentNegotiationManager"/>
<property name="useRegisteredSuffixPatternMatch" value="true"/>
</bean>
<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<property name="favorPathExtension" value="false"/>
<property name="favorParameter" value="true"/>
<property name="mediaTypes">
<value>
json=application/json
xml=application/xml
</value>
</property>
</bean>
我實現,在開源項目Resthub,我的,一組關於這些問題的測試部分:看https://github.com/resthub/resthub-spring-stack/pull/219/files和https://github.com/resthub/resthub-spring-stack/issues/217
感謝響應。我使用了以下標籤: - \t \t \t <屬性名=」 useDefaultSuffixPattern」值= 「假」/> \t 這不工作:( –
user693788
2012-07-30 21:05:29
哦,不知道你是3.0.5,這非常適用於Spring 3.1 ,你可以升級和使用我推薦的東西,我已經測試過這個特定的設置,它按預期工作 – 2012-07-31 01:56:41
謝謝Biju ...它像一個魅力 – user693788 2012-08-01 03:12:54