Spring认为最后一个点后面的任何东西都是文件扩展名,例如.json
或.xml
,然后对其进行结构化以检索您的参数。
因此,如果您有/somepath/{variable}
:
/somepath/param
,/somepath/param.json
,/somepath/param.xml
或/somepath/param.anything
将导致具有值的PARAMparam
/somepath/param.value.json
,/somepath/param.value.xml
否则/somepath/param.value.anything
将导致带有值的参数param.value
如果将映射更改/somepath/{variable:.+}
为建议的值,则任何点(包括最后一个点)都将被视为参数的一部分:
/somepath/param
会带来有价值的参数 param
/somepath/param.json
会带来有价值的参数 param.json
/somepath/param.xml
会带来有价值的参数 param.xml
/somepath/param.anything
会带来有价值的参数 param.anything
/somepath/param.value.json
会带来有价值的参数 param.value.json
- ...
如果您不关心扩展名识别,则可以通过覆盖mvc:annotation-driven
automagic将其禁用:
<bean id="handlerMapping"
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
<property name="contentNegotiationManager" ref="contentNegotiationManager"/>
<property name="useSuffixPatternMatch" value="false"/>
</bean>
因此,如果您有/somepath/{variable}
:
/somepath/param
,/somepath/param.json
,/somepath/param.xml
或/somepath/param.anything
将导致具有值的PARAMparam
/somepath/param.value.json
,/somepath/param.value.xml
否则/somepath/param.value.anything
将导致带有值的参数param.value
注意:与默认配置的区别仅在有类似的映射时可见somepath/something.{variable}
。请参阅Resthub项目问题
如果要保持扩展管理,从Spring 3.2开始,还可以设置RequestMappingHandlerMapping bean的useRegisteredSuffixPatternMatch属性,以保持激活suffixPattern识别,但仅限于已注册的扩展。
在这里,您仅定义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:annotation-driven现在接受contentNegotiation选项以提供自定义bean,但必须将RequestMappingHandlerMapping的属性更改为true(默认为false)(参见https://jira.springsource.org/browse/SPR-7632)。
因此,您仍然必须覆盖所有mvc:annotation驱动的配置。我开了一张去Spring的票,要求自定义RequestMappingHandlerMapping:https : //jira.springsource.org/browse/SPR-11253。如果您感兴趣,请投票。
覆盖时,请谨慎考虑自定义执行管理覆盖。否则,所有自定义Exception映射将失败。您将必须使用list bean重用messageCoverters:
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean" />
<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="conversionService" ref="conversionService" />
<property name="validator" ref="validator" />
</bean>
</property>
<property name="messageConverters" ref="messageConverters"/>
</bean>
<bean id="handlerMapping"
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
</bean>
我在我参与的开源项目Resthub中实现了针对这些主题的一系列测试:请参阅https://github.com/resthub/resthub-spring-stack/pull/219/files和https:// github.com/resthub/resthub-spring-stack/issues/217