Announcement Announcement Module
Collapse
No announcement yet.
[Spring MVC] Problem handling HTTP Request with differents parameters Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • [Spring MVC] Problem handling HTTP Request with differents parameters

    Hello,
    I am going crazy trying to handle the following situation:

    In a view I have the following 3 links that all generate an HTTP Request towards the "/mapping/parameter" folder. These links differ from each other for the parameter passed, so the first one pass a parameter named foo, the second one pass a parameter named foo2 and the last one pass no parameter.

    Code:
    			<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>
    			
    			<li>
    				<a id="byNotParameter" class="textLink" href="<c:url value="/mapping/parameter" />">By path, method, and not presence of parameter</a>
    			</li>
    Now, in my controller class, I would handle this situation using 3 different methods, so I added the following methods to my controller:

    Code:
    	@RequestMapping(value="/mapping/parameter", method=RequestMethod.GET, params="foo")
    	public @ResponseBody String byParameter() {
    		System.out.println("foo");
    		return "Mapped by path + method + presence of query parameter!";
    	}
    	
    	@RequestMapping(value="/mapping/parameter", method=RequestMethod.GET, params="foo2")
    	public @ResponseBody String byParameter2(@RequestParam("foo2") String foo2) {
    		System.out.println("foo2");
    		return "Mapped by path + method + presence of query parameter! (MappingController). Parameter value = " + foo2;
    		}
    	
    	@RequestMapping(value="/mapping/parameter", method=RequestMethod.GET, params="!foo, !foo2")
    	public @ResponseBody String byParameterNegation() {
    		System.out.println("NO foo NO foo2");
    		return "Mapped by path + method + not presence of query parameter!";
    	}
    The problem is that in this way is handled only the !foo, !foo2 case and if I click on the first link (the one with the parameter naming foo) I obtain the following error in the stack trace:

    nov 24, 2012 9:10:29 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.MappingCon troller.byParameter(), public java.lang.String org.springframework.samples.mvc.mapping.MappingCon troller.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.MappingCon troller.byParameter(), public java.lang.String org.springframework.samples.mvc.mapping.MappingCon troller.byParameterNegation()}
    at org.springframework.web.servlet.handler.AbstractHa ndlerMethodMapping.lookupHandlerMethod(AbstractHan dlerMethodMapping.java:262)
    at org.springframework.web.servlet.handler.AbstractHa ndlerMethodMapping.getHandlerInternal(AbstractHand lerMethodMapping.java:212)
    at org.springframework.web.servlet.handler.AbstractHa ndlerMethodMapping.getHandlerInternal(AbstractHand lerMethodMapping.java:55)
    at org.springframework.web.servlet.handler.AbstractHa ndlerMapping.getHandler(AbstractHandlerMapping.jav a: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.p rocessRequest(FrameworkServlet.java:917)
    at org.springframework.web.servlet.FrameworkServlet.d oGet(FrameworkServlet.java:813)
    at javax.servlet.http.HttpServlet.service(HttpServlet .java:621)
    at org.springframework.web.servlet.FrameworkServlet.s ervice(FrameworkServlet.java:798)
    at javax.servlet.http.HttpServlet.service(HttpServlet .java:722)
    at org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invo ke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invo ke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBas e.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(A ccessLogValve.java:929)
    at org.apache.catalina.core.StandardEngineValve.invok e(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.servic e(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.p rocess(AbstractHttp11Processor.java:1002)
    at org.apache.coyote.AbstractProtocol$AbstractConnect ionHandler.process(AbstractProtocol.java:585)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProce ssor.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)

    And if I click on the second link (the one with the parameter naming foo) I obtain the following error in the stack trace:

    nov 24, 2012 9:11:27 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.MappingCon troller.byParameter2(java.lang.String), public java.lang.String org.springframework.samples.mvc.mapping.MappingCon troller.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.MappingCon troller.byParameter2(java.lang.String), public java.lang.String org.springframework.samples.mvc.mapping.MappingCon troller.byParameterNegation()}
    at org.springframework.web.servlet.handler.AbstractHa ndlerMethodMapping.lookupHandlerMethod(AbstractHan dlerMethodMapping.java:262)
    at org.springframework.web.servlet.handler.AbstractHa ndlerMethodMapping.getHandlerInternal(AbstractHand lerMethodMapping.java:212)
    at org.springframework.web.servlet.handler.AbstractHa ndlerMethodMapping.getHandlerInternal(AbstractHand lerMethodMapping.java:55)
    at org.springframework.web.servlet.handler.AbstractHa ndlerMapping.getHandler(AbstractHandlerMapping.jav a: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.p rocessRequest(FrameworkServlet.java:917)
    at org.springframework.web.servlet.FrameworkServlet.d oGet(FrameworkServlet.java:813)
    at javax.servlet.http.HttpServlet.service(HttpServlet .java:621)
    at org.springframework.web.servlet.FrameworkServlet.s ervice(FrameworkServlet.java:798)
    at javax.servlet.http.HttpServlet.service(HttpServlet .java:722)
    at org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invo ke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invo ke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBas e.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(A ccessLogValve.java:929)
    at org.apache.catalina.core.StandardEngineValve.invok e(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.servic e(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.p rocess(AbstractHttp11Processor.java:1002)
    at org.apache.coyote.AbstractProtocol$AbstractConnect ionHandler.process(AbstractProtocol.java:585)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProce ssor.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)

    Why? How can I resolve?

    Thanks
    Andrea

  • #2
    Please use [ code][/code ] tags for the stacktrace.

    I suggest you take a look at the reference guide and the api docs of the RequestMapping annotation... Params takes an array... Each parameter should be an element in that array. You have a single element and that parameter is never present so you basically end up with 2 matches for each link you click...

    So in short make it an array with 2 seperate elements.... ( params={"!foo", "!foo2"} )

    Comment


    • #3
      Originally posted by Marten Deinum View Post
      Please use [ code][/code ] tags for the stacktrace.

      I suggest you take a look at the reference guide and the api docs of the RequestMapping annotation... Params takes an array... Each parameter should be an element in that array. You have a single element and that parameter is never present so you basically end up with 2 matches for each link you click...

      So in short make it an array with 2 seperate elements.... ( params={"!foo", "!foo2"} )
      Ok,
      thank you very much for your clear explanation, now it work well...now I am reading the API doc

      Comment

      Working...
      X