gpt4 book ai didi

java - @RequestMapping 与 "params"在不同类中的相同 URL 导致 "IllegalStateException: Cannot map handler"在 JUnit 与 SpringJUnit4ClassRunner

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:18:16 24 4
gpt4 key购买 nike

我执行重构并将 Controller 拆分为 2 个 Controller :

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

在第一个 Controller 中:

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

在第二个 Controller 中,所以这些注释位于不同的文件中。当我构建和使用项目时,一切都很好(我将 input HTML 标记放在我的表单中,名称不同:firstsecond)。

但是当我尝试运行 JUnit Controller 测试时:

@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")

在第二个 Controller 中,第一个 Controller 的单独测试成功完成。

为了解决这个问题,我可能会使用不同的 URL(@RequestMapping 中的 value),但我不明白为什么请求映射解析为 params> 在我的应用程序生产构建中并因 SpringJUnit4ClassRunner 而失败。

欢迎任何帮助!

附言。我使用 Spring 3.2。

PPS。我发现了大部分相同的问题 Can I have the same mapping value with different param in a different Spring controller?但根据答案,我的生产构建也必须失败?!但是我成功地运行了生产构建!!

另请参阅:

*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 restrictionsthat are enforced at the type level. The primary path mapping (i.e. thespecified URI value) still has to uniquely identify the target handler, withparameter mappings simply expressing preconditions for invoking the handler.

看来我进行了非法编码练习...

最佳答案

这是我在阅读您问题中引用的官方文档时的理解:

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 within the class, with parameter mappings simply expressing preconditions for invoking the handler.

我加了“在类(class)内”这几个字。

请注意在类型级别强制实现。据我了解,这意味着:在 servlet 环境中。在方法级别声明params 与在类型级别声明params 完全相同(至少如果您的类中只有一个方法)。

最后,如果你注意这句话(same source):

When used at the type level, all method-level mappings inherit this parameter restriction (i.e. the type-level restriction gets checked before the handler method is even resolved).

我认为所有这些总结了为什么您没有进行非法编码

关于单元测试:

这里同样重要的是“在 Servlet 环境中。显然,在运行单元测试时:您不在 Servlet 环境中,这可能就是它的原因失败。

关于java - @RequestMapping 与 "params"在不同类中的相同 URL 导致 "IllegalStateException: Cannot map handler"在 JUnit 与 SpringJUnit4ClassRunner,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14561691/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com