org.springframework.web.bind.annotation.support.HandlerMethodInvocationException: Failed to invoke handler method [public java.lang.String net.tv.cs.craft.controller.CategoryController.category(int,org.springframework.ui.Model)]; nested exception is java.lang.IllegalStateException: Could not find @PathVariable [page] in @RequestMapping

Spring JIRA | Tomas Vojtech | 7 years ago
  1. 0

    For controller {code:java} @Controller @RequestMapping("/category") public class CategoryController extends AbstractCsController { @Autowired private ICraftService craftService; @RequestMapping(value = {"/{category}/page/{page}", "/**/{category}/page/{page}"}) public String category(@PathVariable String category, @PathVariable int page, Model model) { model.addAttribute("categories", craftService.findSubcategories(category)); return "craft/category"; } @RequestMapping(value = {"/{category}", "/**/{category}"}) public String category(@PathVariable String category, Model model) { return category(category, 1, model); } @RequestMapping(value = {""}) public String category(Model model) { return category(1, model); } @RequestMapping(value = {"/page/{page}"}) public String category(@PathVariable int page, Model model) { return category(null, page, model); } } {code} I get following error for url like localhost:8080/context/category/page/5 {noformat} org.springframework.web.bind.annotation.support.HandlerMethodInvocationException: Failed to invoke handler method [public java.lang.String net.tv.cs.craft.controller.CategoryController.category(int,org.springframework.ui.Model)]; nested exception is java.lang.IllegalStateException: Could not find @PathVariable [page] in @RequestMapping at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:164) at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:378) at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:366) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:781) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:726) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:636) at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:545) at javax.servlet.http.HttpServlet.service(HttpServlet.java:707) at javax.servlet.http.HttpServlet.service(HttpServlet.java:820) at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:487) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1093) at com.opensymphony.sitemesh.webapp.SiteMeshFilter.obtainContent(SiteMeshFilter.java:125) at com.opensymphony.sitemesh.webapp.SiteMeshFilter.doFilter(SiteMeshFilter.java:76) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1084) at org.springframework.web.filter.ShallowEtagHeaderFilter.doFilterInternal(ShallowEtagHeaderFilter.java:57) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1084) at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1084) at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:360) at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216) at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:181) at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:726) at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:405) at org.mortbay.jetty.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:206) at org.mortbay.jetty.handler.HandlerCollection.handle(HandlerCollection.java:114) at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152) at org.mortbay.jetty.Server.handle(Server.java:324) at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:505) at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:829) at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:514) at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:211) at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:380) at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:395) at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:488) Caused by: java.lang.IllegalStateException: Could not find @PathVariable [page] in @RequestMapping at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter$ServletHandlerMethodInvoker.resolvePathVariable(AnnotationMethodHandlerAdapter.java:660) at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolvePathVariable(HandlerMethodInvoker.java:542) at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:282) at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:156) ... 35 more {noformat} Problem seems to be in AbstractUrlHandlerMapping.lookupHandler. MAybe in code: {code:java} // Pattern match? String bestPathMatch = null; for (String registeredPath : this.handlerMap.keySet()) { if (getPathMatcher().match(registeredPath, urlPath) && (bestPathMatch == null || bestPathMatch.length() < registeredPath.length())) { bestPathMatch = registeredPath; } } {code} After ending this loop the *bestPathMatch* variable has value _/category/\*\*/{category}/_ but it should have _/category/page/{page}_. Because of this code {code:java}Map<String, String> uriTemplateVariables = getPathMatcher().extractUriTemplateVariables(bestPathMatch, urlPath); {code} gives map with {noformat}category -> 5{noformat} entry, but should {noformat}page -> 5{noformat}. Because of the *uriTemplateVariables* doesn't contain the _page_ key it ends with the exception. I think it is a bug because it resolves method handler correctly but the parameter resolution is not correct.

    Spring JIRA | 7 years ago | Tomas Vojtech
    org.springframework.web.bind.annotation.support.HandlerMethodInvocationException: Failed to invoke handler method [public java.lang.String net.tv.cs.craft.controller.CategoryController.category(int,org.springframework.ui.Model)]; nested exception is java.lang.IllegalStateException: Could not find @PathVariable [page] in @RequestMapping
  2. 0

    For controller {code:java} @Controller @RequestMapping("/category") public class CategoryController extends AbstractCsController { @Autowired private ICraftService craftService; @RequestMapping(value = {"/{category}/page/{page}", "/**/{category}/page/{page}"}) public String category(@PathVariable String category, @PathVariable int page, Model model) { model.addAttribute("categories", craftService.findSubcategories(category)); return "craft/category"; } @RequestMapping(value = {"/{category}", "/**/{category}"}) public String category(@PathVariable String category, Model model) { return category(category, 1, model); } @RequestMapping(value = {""}) public String category(Model model) { return category(1, model); } @RequestMapping(value = {"/page/{page}"}) public String category(@PathVariable int page, Model model) { return category(null, page, model); } } {code} I get following error for url like localhost:8080/context/category/page/5 {noformat} org.springframework.web.bind.annotation.support.HandlerMethodInvocationException: Failed to invoke handler method [public java.lang.String net.tv.cs.craft.controller.CategoryController.category(int,org.springframework.ui.Model)]; nested exception is java.lang.IllegalStateException: Could not find @PathVariable [page] in @RequestMapping at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:164) at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:378) at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:366) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:781) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:726) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:636) at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:545) at javax.servlet.http.HttpServlet.service(HttpServlet.java:707) at javax.servlet.http.HttpServlet.service(HttpServlet.java:820) at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:487) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1093) at com.opensymphony.sitemesh.webapp.SiteMeshFilter.obtainContent(SiteMeshFilter.java:125) at com.opensymphony.sitemesh.webapp.SiteMeshFilter.doFilter(SiteMeshFilter.java:76) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1084) at org.springframework.web.filter.ShallowEtagHeaderFilter.doFilterInternal(ShallowEtagHeaderFilter.java:57) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1084) at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1084) at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:360) at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216) at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:181) at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:726) at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:405) at org.mortbay.jetty.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:206) at org.mortbay.jetty.handler.HandlerCollection.handle(HandlerCollection.java:114) at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152) at org.mortbay.jetty.Server.handle(Server.java:324) at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:505) at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:829) at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:514) at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:211) at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:380) at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:395) at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:488) Caused by: java.lang.IllegalStateException: Could not find @PathVariable [page] in @RequestMapping at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter$ServletHandlerMethodInvoker.resolvePathVariable(AnnotationMethodHandlerAdapter.java:660) at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolvePathVariable(HandlerMethodInvoker.java:542) at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:282) at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:156) ... 35 more {noformat} Problem seems to be in AbstractUrlHandlerMapping.lookupHandler. MAybe in code: {code:java} // Pattern match? String bestPathMatch = null; for (String registeredPath : this.handlerMap.keySet()) { if (getPathMatcher().match(registeredPath, urlPath) && (bestPathMatch == null || bestPathMatch.length() < registeredPath.length())) { bestPathMatch = registeredPath; } } {code} After ending this loop the *bestPathMatch* variable has value _/category/\*\*/{category}/_ but it should have _/category/page/{page}_. Because of this code {code:java}Map<String, String> uriTemplateVariables = getPathMatcher().extractUriTemplateVariables(bestPathMatch, urlPath); {code} gives map with {noformat}category -> 5{noformat} entry, but should {noformat}page -> 5{noformat}. Because of the *uriTemplateVariables* doesn't contain the _page_ key it ends with the exception. I think it is a bug because it resolves method handler correctly but the parameter resolution is not correct.

    Spring JIRA | 7 years ago | Tomas Vojtech
    org.springframework.web.bind.annotation.support.HandlerMethodInvocationException: Failed to invoke handler method [public java.lang.String net.tv.cs.craft.controller.CategoryController.category(int,org.springframework.ui.Model)]; nested exception is java.lang.IllegalStateException: Could not find @PathVariable [page] in @RequestMapping
  3. 0

    Problem with RequestMapping and PathVariable - Spring Forum

    spring.io | 11 months ago
    org.springframework.web.bind.annotation.support.HandlerMethodInvocationException: Failed to invoke handler method [public void se.leanon.dojotime.web.controller.JsonRestController.get(java.lang.String)]; nested exception is java.lang.IllegalStateException: Could not find @PathVariable [task] in @RequestMapping
  4. Speed up your debug routine!

    Automated exception search integrated into your IDE

  5. 0

    Display PDF in a webpage

    Stack Overflow | 3 years ago | newbie
    java.lang.IllegalStateException: Could not find @PathVariable [id] in @RequestMapping
  6. 0

    How to unit test a Spring MVC controller using @PathVariable?

    Stack Overflow | 7 years ago | Martiner
    java.lang.IllegalStateException: Could not find @PathVariable [id] in @RequestMapping

    2 unregistered visitors
    Not finding the right solution?
    Take a tour to get the most out of Samebug.

    Tired of useless tips?

    Automated exception search integrated into your IDE

    Root Cause Analysis

    1. java.lang.IllegalStateException

      Could not find @PathVariable [page] in @RequestMapping

      at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter$ServletHandlerMethodInvoker.resolvePathVariable()
    2. Spring MVC
      AnnotationMethodHandlerAdapter$ServletHandlerMethodInvoker.resolvePathVariable
      1. org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter$ServletHandlerMethodInvoker.resolvePathVariable(AnnotationMethodHandlerAdapter.java:660)
      1 frame
    3. Spring
      HandlerMethodInvoker.invokeHandlerMethod
      1. org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolvePathVariable(HandlerMethodInvoker.java:542)
      2. org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:282)
      3. org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:156)
      3 frames
    4. Spring MVC
      FrameworkServlet.doGet
      1. org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:378)
      2. org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:366)
      3. org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:781)
      4. org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:726)
      5. org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:636)
      6. org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:545)
      6 frames
    5. JavaServlet
      HttpServlet.service
      1. javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
      2. javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
      2 frames
    6. Jetty Server
      ServletHandler$CachedChain.doFilter
      1. org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:487)
      2. org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1093)
      2 frames
    7. Sitemesh
      SiteMeshFilter.doFilter
      1. com.opensymphony.sitemesh.webapp.SiteMeshFilter.obtainContent(SiteMeshFilter.java:125)
      2. com.opensymphony.sitemesh.webapp.SiteMeshFilter.doFilter(SiteMeshFilter.java:76)
      2 frames
    8. Jetty Server
      ServletHandler$CachedChain.doFilter
      1. org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1084)
      1 frame
    9. Spring
      OncePerRequestFilter.doFilter
      1. org.springframework.web.filter.ShallowEtagHeaderFilter.doFilterInternal(ShallowEtagHeaderFilter.java:57)
      2. org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
      2 frames
    10. Jetty Server
      ServletHandler$CachedChain.doFilter
      1. org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1084)
      1 frame
    11. Spring
      OncePerRequestFilter.doFilter
      1. org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
      2. org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
      2 frames
    12. Jetty Server
      SelectChannelEndPoint.run
      1. org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1084)
      2. org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:360)
      3. org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
      4. org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:181)
      5. org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:726)
      6. org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:405)
      7. org.mortbay.jetty.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:206)
      8. org.mortbay.jetty.handler.HandlerCollection.handle(HandlerCollection.java:114)
      9. org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
      10. org.mortbay.jetty.Server.handle(Server.java:324)
      11. org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:505)
      12. org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:829)
      13. org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:514)
      14. org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:211)
      15. org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:380)
      16. org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:395)
      16 frames
    13. Jetty Util
      QueuedThreadPool$PoolThread.run
      1. org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:488)
      1 frame