Java @使用“请求映射”;“参数”;在不同类中的同一URL上导致“;IllegalStateException:无法映射处理程序";在JUnit中使用SpringJUnit4 ClassRunner

Java @使用“请求映射”;“参数”;在不同类中的同一URL上导致“;IllegalStateException:无法映射处理程序";在JUnit中使用SpringJUnit4 ClassRunner,java,spring,spring-mvc,junit,Java,Spring,Spring Mvc,Junit,我执行重构并将控制器拆分为2个控制器,包括: @RequestMapping(value = "/graph.htm", method = RequestMethod.POST, params="first") @RequestMapping(value=“/graph.htm”,method=RequestMethod.POST,params=“first”) 在第一个控制器和: @RequestMapping(value = "/graph.htm", method = RequestMet

我执行重构并将控制器拆分为2个控制器,包括:

@RequestMapping(value = "/graph.htm", method = RequestMethod.POST, params="first") @RequestMapping(value=“/graph.htm”,method=RequestMethod.POST,params=“first”) 在第一个控制器和:

@RequestMapping(value = "/graph.htm", method = RequestMethod.POST, params="second") @RequestMapping(value=“/graph.htm”,method=RequestMethod.POST,params=“second”) 在第二个控制器中,这些注释位于不同的文件中。当我构建和使用project时,一切正常(我在表单中使用不同的名称放置inputHTML标记:第一个和第二个)

但当我尝试运行JUnit控制器测试时:

@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath:test-context.xml" }) @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(位置={“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) 原因:java.lang.IllegalStateException:无法映射处理程序“firstController” 到URL路径[/graph.htm]:已存在处理程序 已映射[class com.web.controller.SecondController]类型的。 位于org.springframework.web.servlet.handler.AbstractUrlHandlerMapping.registerHandler(AbstractUrlHandlerMapping.java:294) 位于org.springframework.web.servlet.handler.AbstractUrlHandlerMapping.registerHandler(AbstractUrlHandlerMapping.java:266) 位于org.springframework.web.servlet.handler.AbstractDetectingUrlHandlerMapping.detectHandlers(AbstractDetectingUrlHandlerMapping.java:82) 位于org.springframework.web.servlet.handler.AbstractDetectingUrlHandlerMapping.initApplicationContext(AbstractDetectingUrlHandlerMapping.java:58) 位于org.springframework.context.support.ApplicationObjectSupport.initApplicationContext(ApplicationObjectSupport.java:119) 位于org.springframework.web.context.support.WebApplicationObjectSupport.initApplicationContext(WebApplicationObjectSupport.java:72) 位于org.springframework.context.support.ApplicationObjectSupport.setApplicationContext(ApplicationObjectSupport.java:73) 位于org.springframework.context.support.ApplicationContextawareReprocessor.InvokeAwareInterface(ApplicationContextawareReprocessor.java:117) 在org.springframework.context.support.ApplicationContextawareReprocessor.postProcessBeforeInitialization(ApplicationContextawareReprocessor.java:92)上 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:399)上 位于org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1479) 位于org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:524) 当我评论这一点时:

@RequestMapping(value = "/graph.htm", method = RequestMethod.POST, params="second") @RequestMapping(value=“/graph.htm”,method=RequestMethod.POST,params=“second”) 在第二个控制器中,第一个控制器的单独测试成功完成

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

欢迎任何帮助

PS。我使用Spring3.2

PPS。我发现几乎相同的问题,但根据答案,我的生产建设也必须失败?!但是我成功地运行了生产构建

另请参阅:

*PPS

我检查3.2的官方文件:

在Servlet环境中,参数映射被视为限制 在类型级别强制执行的。主路径映射(即 指定的URI值)仍然必须唯一标识目标处理程序,并且 参数映射只是表示调用处理程序的先决条件。
看来我是在进行非法编码…

这是我在阅读你问题中引用的官方文件时所理解的:

在Servlet环境中,参数映射被视为限制 在类型级别强制执行的。主路径映射(即 指定的URI值)仍然必须使用 参数映射只是表示调用处理程序的先决条件

我在课堂上添加了单词“

请注意在类型级别强制执行的。据我所知,这意味着:在servlet环境中。在方法级别声明参数与在类型级别声明参数完全相同(至少在类中只有一个方法的情况下)

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

当在类型级别使用时,所有方法级别映射都继承此参数限制(即,在处理程序方法被解析之前,类型级别限制被选中)

我认为所有这些总结了为什么你没有做非法编码

关于单元测试:


这里同样重要的是“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类使用不同的映射模式。旧的每个类使用,新的每个方法使用

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