aspose file tools*
The moose likes Spring and the fly likes Problem handling HTTP Request with different parameters Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Problem handling HTTP Request with different parameters" Watch "Problem handling HTTP Request with different parameters" New topic
Author

Problem handling HTTP Request with different parameters

Andrea Nobili
Greenhorn

Joined: Nov 08, 2012
Posts: 21
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.



Now, in my controller class, I would handle this situation using 3 different methods, so I added the following methods to my controller:



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.MappingController.byParameter(), 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(), 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)

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.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)

Why? How can I resolve?

Thanks
Andrea









Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17256
    
    6

You can't put two params like that in params=""

But you could do it in code. Pass in the HttpServletRequest and look yourself.

Or have the first two methods, then the third not have params in it. It might be that the first two are checked first then the third. And in the third pass HttpServletRequest and make sure they aren't in there.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Andrea Nobili
Greenhorn

Joined: Nov 08, 2012
Posts: 21
Mark Spritzler wrote:You can't put two params like that in params=""

But you could do it in code. Pass in the HttpServletRequest and look yourself.

Or have the first two methods, then the third not have params in it. It might be that the first two are checked first then the third. And in the third pass HttpServletRequest and make sure they aren't in there.

Mark


mmm, ok...so I have undestrand that I can not have something like params="!foo, !foo2".

But I have not understand how can I solve this situation, can you help me? :-(

Thanks
Andrea
Andrea Nobili
Greenhorn

Joined: Nov 08, 2012
Posts: 21
Ok,
doing so seem to work well:



So I have a method that handle only the http request towards "mapping/parameter" and two other method for the same folder with one parameter: foo and foo2

I have an other theorical doubt probably most related about Java rather than about Spring framework

You have give me this link: http://docs.oracle.com/javaee/6/api/javax/servlet/ServletRequest.html#getParameter%28java.lang.String%29
This link is the Java Docs and there is nothig about Spring framework docs.

Now I have study the getParameter method documentation and I have see that this method belongs to java.lang.String that is one of the standard Java library...

So it means that when in my code I use the annotation: @RequestParam("foo2") String foo2 the Spring framework call for me the getParameter method from the java.lang.String class and put this value inside a new String variable? I have well understand?

In practice when I call a Spring annotation, Spring framework call form me some his method that do something? (in this case: call the java.lang.String.getParameter() method and put the returned value inside a String variabile whose name is declared by the developer in the annotation body)

Thank you very much for your help
Andrea

Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17256
    
    6

I didn't give you any link. In my post, I entered in nothing of a link. But it looks like the browser or something else, which happens a lot places on the internet, did a search or created a popup with a link. I did not put that there.

getParameter is not on the String class, it returns a String, but is in the HttpServletRequest class.

And @RequestParam means Spring will go into the HttpServletRequest and call getParameter on that class for you. It will also convert the String that returns to the type of the parameter in your Controller method.

Mark
Andrea Nobili
Greenhorn

Joined: Nov 08, 2012
Posts: 21
Mark Spritzler wrote:I didn't give you any link. In my post, I entered in nothing of a link. But it looks like the browser or something else, which happens a lot places on the internet, did a search or created a popup with a link. I did not put that there.

getParameter is not on the String class, it returns a String, but is in the HttpServletRequest class.

And @RequestParam means Spring will go into the HttpServletRequest and call getParameter on that class for you. It will also convert the String that returns to the type of the parameter in your Controller method.

Mark


Ok,
now it is very clear :-)

Tnx
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem handling HTTP Request with different parameters