我在 Spring MVC 中获取 HTTP 请求的参数时遇到问题。
在我看来,我有以下链接:
<li>
<a id="byParameter2" class="textLink"
href="<c:url value="/mapping/parameter?myparam=blabla" />">(PERSONALE) By path, method, and presence of parameter with value="blabla"</a>
</li>
这是我的控制器类代码:
package org.springframework.samples.mvc.mapping;
import javax.servlet.http.HttpServletRequest;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class MappingController {
/* Gestisce HTTP Request di tipo GET che contengono un parametro di nome "foo" verso il percorso "/mapping/parameter"
* Restituisce una stringa nel campo body della HTTP Response
*/
@RequestMapping(value="/mapping/parameter", method=RequestMethod.GET, params="foo")
public @ResponseBody String byParameter() {
return "Mapped by path + method + presence of query parameter!";
}
/* (PERSONALE) Gestisce richieste HTTP di tipo GET verso il path "/mapping/parameter" solo se NON contengono un parametro di input di nome "myparam"
* e ne estrae il valore */
@RequestMapping(value="/mapping/parameter", method=RequestMethod.GET, params="myparam")
public @ResponseBody String byParameter(@RequestParam("myparam") String myparam) {
return "Mapped by path + method + presence of query parameter! (MappingController). Parameter value = " + myparam;
}
/* Gestisce HTTP Request di tipo GET che NON contengono un parametro di nome "foo" verso il percorso "/mapping/parameter"
* Restituisce una stringa nel campo body della HTTP Response
*/
@RequestMapping(value="/mapping/parameter", method=RequestMethod.GET, params="!foo")
public @ResponseBody String byParameterNegation() {
return "Mapped by path + method + not presence of query parameter!";
}
}
问题是,当我运行我的应用程序时,如果我单击生成针对"/mapping/parameter?myparam=blabla"
此请求的 HTTP 请求的链接,我的方法不会处理我的public @ResponseBody String byParameter(@RequestParam("myparam") String myparam)
方法,并且我在浏览器中有以下错误消息:
**HTTP Status 500 - Request processing failed; nested exception is java.lang.IllegalStateException: Ambiguous handler methods mapped for HTTP path 'http://localhost:8080/spring-mvc-showcase/mapping/parameter': {public java.lang.String org.springframework.samples.mvc.mapping.MappingController.byParameter2(java.lang.String), public java.lang.String org.springframework.samples.mvc.mapping.MappingController.byParameterNegation()}**
And the following error in the stack trace:
**SEVERE: Servlet.service() for servlet [appServlet] in context with path [/spring-mvc-showcase] threw exception [Request processing failed; nested exception is java.lang.IllegalStateException: Ambiguous handler methods mapped for HTTP path 'http://localhost:8080/spring-mvc-showcase/mapping/parameter': {public java.lang.String org.springframework.samples.mvc.mapping.MappingController.byParameter2(java.lang.String), public java.lang.String org.springframework.samples.mvc.mapping.MappingController.byParameterNegation()}] with root cause
java.lang.IllegalStateException: Ambiguous handler methods mapped for HTTP path 'http://localhost:8080/spring-mvc-showcase/mapping/parameter': {public java.lang.String org.springframework.samples.mvc.mapping.MappingController.byParameter2(java.lang.String), public java.lang.String org.springframework.samples.mvc.mapping.MappingController.byParameterNegation()}
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.lookupHandlerMethod(AbstractHandlerMethodMapping.java:262)
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.getHandlerInternal(AbstractHandlerMethodMapping.java:212)
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.getHandlerInternal(AbstractHandlerMethodMapping.java:55)
at org.springframework.web.servlet.handler.AbstractHandlerMapping.getHandler(AbstractHandlerMapping.java:297)
at org.springframework.web.servlet.DispatcherServlet.getHandler(DispatcherServlet.java:1091)
at org.springframework.web.servlet.DispatcherServlet.getHandler(DispatcherServlet.java:1076)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:896)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:917)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:813)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:798)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1002)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)**
为什么?问题出在哪里?我该如何解决?