2012-07-25 52 views
2

我有兩個URL請求春天@RequestMapping不工作如果在URL擴展

/domain/entity/list.vendorname /域/實體/列表

在控制器的映射預期: -

@RequestMapping(value = "/entity/list.{vendorId}") 

@RequestMapping(value = "/entity/list") 

問題: -

我的URL: - /domain/entity/list.vendorname

正被映射到 @RequestMapping(value = "/entity/list")

方法而不是另一個。

請幫幫

回答

2

原因是默認指找到用於請求URL的映射組件 - RequestMappingHandlerMapping,上後綴還(/entity/list/entity/list.*被認爲是等價的修復可以是轉動後綴匹配關閉匹配。:

<bean name="handlerMapping" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"> 
    <property name="useSuffixPatternMatch" value="false"></property> 
</bean> 

然而,這也意味着你將不得不手動註冊的HandlerAdapter也 - 如果你使用<mvc:annotation-driven/>你可以沿着這些路線的東西替代它:

<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> 

      </property> 
     </bean> 
    </property> 
    <property name="messageConverters"> 
     <list> 
      <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> 
     </list> 
    </property> 
</bean> 
+0

感謝響應。我使用了以下標籤: - \t \t \t <屬性名=」 useDefaultSuffixPattern」值= 「假」/> \t 這不工作:( – user693788 2012-07-30 21:05:29

+0

哦,不知道你是3.0.5,這非常適用於Spring 3.1 ,你可以升級和使用我推薦的東西,我已經測試過這個特定的設置,它按預期工作 – 2012-07-31 01:56:41

+0

謝謝Biju ...它像一個魅力 – user693788 2012-08-01 03:12:54

0

從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/fileshttps://github.com/resthub/resthub-spring-stack/issues/217