Java – How to manage two HTTP Request towards the same folder but with differents HTTP Parameters in Spring MVC

httprequestjavaparametersspringspring-mvc

I am studing the Spring MVC showcase example and I ask you some simple explanation about the request mapping topic.

In pratice, in my view I have the following two links:

        <li>
            <a id="byParameter" class="textLink" href="<c:url value="/mapping/parameter?foo=bar" />">By path, method, and presence of parameter</a>
        </li>

        <!-- PERSONALE -->
        <li>
            <a id="byParameter2" class="textLink" href="<c:url value="/mapping/parameter?foo2=blabla" />">(PERSONALE) By path, method, and presence of parameter with value="blabla"</a>
        </li>

Either generate an HTTP Request towards the "/mapping/parameter/" folder but they have different parameter

The first one have a parameter named foo and the second one have a parameter named foo2

Ok…now I would that: the HTTP Request generate by the first link (the one with parameter named "foo") is handled by a method and the HTTP Request generate by the second link (the one with parameter named "foo2") is handled by an other method

So, in my controller class, I thought to do in this way adding the following 2 methods:

@RequestMapping(value="/mapping/parameter", method=RequestMethod.GET, params="foo")
public @ResponseBody String byParameter() {
    return "Mapped by path + method + presence of query parameter!";
}

@RequestMapping(value="/mapping/parameter", method=RequestMethod.GET, params="foo2")
public @ResponseBody String byParameter(@RequestParam("foo2") String foo2) {
    return "Mapped by path + method + presence of query parameter! (MappingController). Parameter value = " + foo2;
    }

(The second pick up in its code also the parameter value, but I think that this is not important)

The problem is that when I click on the second link I obtain an error and in the stack trace I have the following error message:

nov 24, 2012 7:56:42 PM org.apache.catalina.core.StandardWrapperValve invoke
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.byParameter(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.byParameter(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)

It seems that the application is not able to understand which method to use for the path "/mapping/parameter" but why?

The first method is annoted with a @RequestMapping annotation that explicitly specify that have to handle HTTP Request towards "/mapping/parameter" folder and having parameter named foo, the second is annoteth with a @RequestMapping that say that have to handle HTTP Request towards "/mapping/parameter" folder and having parameter named foo2…I do not think an ambiguous situation

I think that the problem is not that the 2 methods have the same name because they have different signatures so they are 2 different methods…(I have also tried to renamen the second one but the problem remains)

Also, a strange think is that if I click on the first link (the one that generate the HTTP Request with parameter named "foo"), it is correctly handled by the first method…why this is not ambiguous ?!?!

How can I solve?

Thank you very much

Andrea

Best Answer

Your Code is correct! The Problem must be somewere else in the code. I have build a simple Project and copied your snippets, and it worked without any problem!

You can download my sample from here (it is a Eclipse Maven Project).

I think your problem is that you may have overlooked a third method or an second controller.