簡體   English   中英

@RequestMapping與不同類中相同URL的“params”導致JUnit中的“IllegalStateException:無法映射處理程序”與SpringJUnit4ClassRunner

[英]@RequestMapping with “params” on same URL in different classes cause “IllegalStateException: Cannot map handler” in JUnit with SpringJUnit4ClassRunner

我執行重構並將控制器拆分為2個控制器:

@RequestMapping(value = "/graph.htm", method = RequestMethod.POST, params="first")

在第一個控制器和:

@RequestMapping(value = "/graph.htm", method = RequestMethod.POST, params="second")

在第二個控制器中,所以這些注釋位於不同的文件中。 當我構建和使用項目時,一切都很好(我在表單中輸入了不同名稱的輸入 HTML標記: 第一個第二個 )。

但是當我嘗試運行JUnit控制器測試時:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:test-context.xml" })

我得到了痕跡:

Caused by: java.lang.IllegalStateException: Cannot map handler 'firstController'
  to URL path [/graph.htm]: There is already handler
  of type [class com.web.controller.SecondController] mapped.
    at org.springframework.web.servlet.handler.AbstractUrlHandlerMapping.registerHandler(AbstractUrlHandlerMapping.java:294)
    at org.springframework.web.servlet.handler.AbstractUrlHandlerMapping.registerHandler(AbstractUrlHandlerMapping.java:266)
    at org.springframework.web.servlet.handler.AbstractDetectingUrlHandlerMapping.detectHandlers(AbstractDetectingUrlHandlerMapping.java:82)
    at org.springframework.web.servlet.handler.AbstractDetectingUrlHandlerMapping.initApplicationContext(AbstractDetectingUrlHandlerMapping.java:58)
    at org.springframework.context.support.ApplicationObjectSupport.initApplicationContext(ApplicationObjectSupport.java:119)
    at org.springframework.web.context.support.WebApplicationObjectSupport.initApplicationContext(WebApplicationObjectSupport.java:72)
    at org.springframework.context.support.ApplicationObjectSupport.setApplicationContext(ApplicationObjectSupport.java:73)
    at org.springframework.context.support.ApplicationContextAwareProcessor.invokeAwareInterfaces(ApplicationContextAwareProcessor.java:117)
    at org.springframework.context.support.ApplicationContextAwareProcessor.postProcessBeforeInitialization(ApplicationContextAwareProcessor.java:92)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:399)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1479)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:524)

當我發表評論時:

@RequestMapping(value = "/graph.htm", method = RequestMethod.POST, params="second")

在第二個控制器單獨測試第一個控制器成功完成。

要解決此問題,我可能會使用不同的URL( @RequestMapping中的 ),但我不明白為什么請求映射在我的應用程序的生成版本中解析為params並且使用SpringJUnit4ClassRunner失敗。

歡迎任何幫助!

PS 我使用Spring 3.2。

PPS 我發現大多數相同的問題我可以在不同的Spring控制器中使用不同的param具有相同的映射值嗎? 但根據答案,我的生產構建也必須失敗?! 但是我成功運行了生產!

另請參閱:

* PPS

我檢查3.2的官方文檔:

http://static.springsource.org/spring/docs/3.2.x/javadoc-api/org/springframework/web/bind/annotation/RequestMapping.html#params%28%29

In a Servlet environment, parameter mappings are considered as restrictions
that are enforced at the type level. The primary path mapping (i.e. the
specified URI value) still has to uniquely identify the target handler, with
parameter mappings simply expressing preconditions for invoking the handler.

所以我似乎在進行非法編碼練習......

這是我在閱讀您的問題中引用的官方文檔時所理解的:

在Servlet環境中,參數映射被視為在類型級別強制執行的限制。 主路徑映射(即,指定的URI值)仍具有唯一地標識類內的目標的處理程序,與參數映射簡單地表達用於調用處理程序的先決條件。

我添加了“ 在課堂內 ”的字樣。

請注意在類型級別強制執行 據我所知,它意味着:在servlet環境中。 在方法層次上聲明params為完全一樣的類型層次上聲明PARAMS(至少如果你僅僅只有一個在你的類中的方法)。

最后,如果你注意這句話( 同一來源 ):

在類型級別使用時,所有方法級別映射都繼承此參數限制(即在處理器方法甚至解析之前檢查類型級別限制)。

我想所有這些都總結了為什么你不進行非法編碼

關於單元測試:

這里同樣重要的是“ 在Servlet環境中 。顯然,在運行單元測試時:您不在Servlet環境中 ,這可能是它失敗的原因。

我的鄰居同事幫我調試問題。

我們比較生產和測試環境,發現上下文XML配置的差異。

以前的測試配置失敗:

<bean name="handlerMapping"
          class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
    <bean name="handlerAdapter"
          class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>

新的和工作的測試上下文配置:

<bean name="handlerMapping"
          class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
    <bean name="handlerAdapter"
          class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>

不同的spring類使用不同的映射模式。 每個類的舊用法,每種方法更新用途!!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM