Announcement Announcement Module
Collapse
No announcement yet.
Problem with RequestMapping and PathVariable Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problem with RequestMapping and PathVariable

    I'm having a problem getting path variables to work in Spring 3.0.0. I keep getting this error:

    java.lang.IllegalStateException: Could not find @PathVariable [task] in @RequestMapping

    This is my test controller:

    Code:
    @Controller
    public class JsonRestController  {
    	
    	private static final Log LOG = LogFactory.getLog(JsonRestController.class);
    	
    	@RequestMapping("/tasks/{task}")
    	public void get(@PathVariable("task") String task) {
    		
    		LOG.debug("Task controller with task:" + task);
    
    		
    	}
    
    }
    Clearly it is finding the right method in the the right class and trying to execute it, but failing. What am I doing wrong?

    Heres the full stack:

    Code:
    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
    	at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:171)
    	at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:414)
    	at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:402)
    	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:771)
    	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716)
    	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:647)
    	at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:552)
    	at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    	at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
    	at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)
    	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
    	at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    	at java.lang.Thread.run(Thread.java:637)
    Caused by: java.lang.IllegalStateException: Could not find @PathVariable [task] in @RequestMapping
    	at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter$ServletHandlerMethodInvoker.resolvePathVariable(AnnotationMethodHandlerAdapter.java:744)
    	at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolvePathVariable(HandlerMethodInvoker.java:599)
    	at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:289)
    	at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:163)
    	... 20 more

  • #2
    Do you have another handler in the same or another controller with the same mapping, or have you renamed your class and not done a clean (so there's > 1 mapping)?

    I ask because I just found this post.

    PUK

    Comment


    • #3
      Thanks for the reply. I saw that post too, but no, I have no conflicting controllers. I can see in the logs that Spring is finding the right class and the right method, its just unable to execute it.

      I have the same problem with other controllers (mapped to other URLs).

      Mark.

      Comment


      • #4
        Problem solved, kind of. I updated my spring bean config to use autowiring and component discovery, and now it works. A bit mysterious, but there you are.

        Comment


        • #5
          I have the same problem

          How exactly did you solve this problem, cos I have the same problem... thanks

          Comment


          • #6
            Originally posted by Jeroen Rosenberg View Post
            How exactly did you solve this problem, cos I have the same problem... thanks
            Not sure of the above poster. But you'll need to add the following in your spring config file.

            <context:component-scan base-package="<your package for the controller class where you have the annotation>" />

            <bean class="org.springframework.web.servlet.mvc.annotat ion.DefaultAnnotationHandlerMapping" />

            <bean class="org.springframework.web.servlet.mvc.annotat ion.AnnotationMethodHandlerAdapter" />

            Comment


            • #7
              Path Variable Problem

              Originally posted by excogitator View Post
              Not sure of the above poster. But you'll need to add the following in your spring config file.

              <context:component-scan base-package="<your package for the controller class where you have the annotation>" />

              <bean class="org.springframework.web.servlet.mvc.annotat ion.DefaultAnnotationHandlerMapping" />

              <bean class="org.springframework.web.servlet.mvc.annotat ion.AnnotationMethodHandlerAdapter" />
              I have the Same problem while i am testing my controller.

              here is my controller method mapping

              @RequestMapping(value = "/join/{applicationId}/{gameId}/{gameType}", method = RequestMethod.GET)
              @ResponseBody
              public OnDemandGameResponse joinGame(@PathVariable("applicationId") String applicationId, @PathVariable("gameId") long gameId,
              @PathVariable("gameType") GameType gameType) {


              and here is the Test Code
              @Before
              public void setUp() throws Exception {
              request = new MockHttpServletRequest();
              response = new MockHttpServletResponse();
              ApplicationContext ctx = new FileSystemXmlApplicationContext(new String[]{System.getProperty("user.dir")
              + "/web/WEB-INF/application-contexts/application-context-root.xml",
              System.getProperty("user.dir")+ "/web/WEB-INF/application-contexts/restcontrollers.xml",
              System.getProperty("user.dir")+ "/web/WEB-INF/application-contexts/controllers.xml",
              System.getProperty("user.dir")+ "/web/WEB-INF/valangConfig.xml"});
              onDemandGameController = (OnDemandGameController) ctx.getBean("onDemandGameController");
              handler=(AnnotationMethodHandlerAdapter)ctx.getBea n("annotationMethodHandlerAdapter");
              }

              @Test
              public void handleJionGameRequest() throws Exception {
              request.setRequestURI("/ondemandgame/join/123/13/ONDEMAND");
              request.setMethod("GET");
              request.addHeader("Accept", "application/xml");
              request.addHeader("content-type", "application/xml");
              handler.handle(request, response, onDemandGameController);

              Assert.assertEquals("The response content type is not application/xml", "application/xml", response.getContentType());
              }


              the configuration are fine at all the places

              Comment

              Working...
              X