1

i am just a newbie to the spring MVC following is my code,When i try to go to bye i get following error

Could not find @PathVariable [pathVars] in @RequestMapping Spring MVC

Following is my code

package com.springapp.mvc;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

@Controller
public class HelloController {
    @RequestMapping("/")
    public String printWelcome(ModelMap model) {
        model.addAttribute("message", "Hello world!");
        return "hello";
    }
    @RequestMapping("/runThis/{bye}/{hye}")
    public ModelAndView printBye(@PathVariable Map<String,String> pathVars) {
        String Bye =  pathVars.get("bye");
        String Hye =  pathVars.get("hye");
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("message", "you are"+Bye+ "AND Here COmes" +Hye+"!");
        return modelAndView;
    }
}

EDIT Full Stack.

type Exception report

message Request processing failed; nested exception is org.springframework.web.bind.annotation.support.HandlerMethodInvocationException: Failed to invoke handler method [public org.springframework.web.servlet.ModelAndView com.springapp.mvc.HelloController.printBye(java.util.Map)]; nested exception is java.lang.IllegalStateException: Could not find @PathVariable [pathVars] in @RequestMapping

description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.web.bind.annotation.support.HandlerMethodInvocationException: Failed to invoke handler method [public org.springframework.web.servlet.ModelAndView com.springapp.mvc.HelloController.printBye(java.util.Map)]; nested exception is java.lang.IllegalStateException: Could not find @PathVariable [pathVars] in @RequestMapping
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:978)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:723)

root cause

org.springframework.web.bind.annotation.support.HandlerMethodInvocationException: Failed to invoke handler method [public org.springframework.web.servlet.ModelAndView com.springapp.mvc.HelloController.printBye(java.util.Map)]; nested exception is java.lang.IllegalStateException: Could not find @PathVariable [pathVars] in @RequestMapping
    org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:180)
    org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:446)
    org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:434)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:723)

root cause

java.lang.IllegalStateException: Could not find @PathVariable [pathVars] in @RequestMapping
    org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter$ServletHandlerMethodInvoker.resolvePathVariable(AnnotationMethodHandlerAdapter.java:859)
    org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolvePathVariable(HandlerMethodInvoker.java:710)
    org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:359)
    org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:170)
    org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:446)
    org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:434)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:723)

note The full stack trace of the root cause is available in the Apache Tomcat/6.0.43 logs.
7
  • 2
    What do you think @PathVariable does? Commented Jan 15, 2015 at 18:39
  • i assume path Variable is used to map the request to the respected Action. I know my pathVars are empty at the moment but even if i make some mapper for them i still get same error. Commented Jan 15, 2015 at 18:41
  • 1
    Don't assume. Read the documentation. Commented Jan 15, 2015 at 18:42
  • @SotiriosDelimanolis:Please Check the code i have made a small modification i still get the same error, Please help me here. Commented Jan 15, 2015 at 18:45
  • 3
    You are using the wrong infrastructure components. I suspect you either are missing <mvc:annotation-driven /> or @EnableWebMvc when using java config. Looks like you are using the DispatcherServlet defaults which uses the old infrastructure AnnotationMethodHandlerAdapter instead of the RequestMappingHandlerAdapter which has support for this. Commented Jan 15, 2015 at 19:13

4 Answers 4

7

you need <mvc:annotation-driven/> in your dispatcher-servlet config xml

or in the later version of spring use

@EnableWebMvc annotation in your spring config class

and please understand @EnableWebMvc is made to replace the mvc:annotation-driven

and making sure the xmlns are correct in your

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
                    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd     
                    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
Sign up to request clarification or add additional context in comments.

Comments

1

Instead of this in your code:

public ModelAndView printBye(@PathVariable Map<String,String> pathVars) {

Change it to this:

public ModelAndView printBye(@RequestParam Map<String,String> pathVars) {

This is because @RequestParam is used to deal with getting a Map object passed into the parameter while @PathVariable is used to deal with individual items getting passed into the paramter. Here is an example of @PathVariable:

public ModelAndView printBye(@PathVariable("bye") String Bye, @PathVariable("hye") String Hye) {

Comments

0

With your request mapping you indicate that your URL has a fixed part /runThis/ and two variable parts {bye} and {hye} and that you want them to be mapped to two parameters of your method (with matching names as you don't indicate anything else).

But in your method you declare a single parameter named pathVars. So you have a mismatch in the names, types and number of parameters. That's what Spring MVC complains about.

So what you want is:

@RequestMapping("/runThis/{bye}/{hye}")
public ModelAndView printBye(@PathVariable String bye, @PathVariable String hye) {
    ModelAndView modelAndView = new ModelAndView();
    modelAndView.addObject("message", "you are" + bye + "AND Here COmes" + hye + "!");
    return modelAndView;
}

It's even simpler and more type safe than your code. And please read the documentation. This is a very basic example.

4 Comments

If the method parameter is Map<String, String> or MultiValueMap<String, String> then the map is populated with all path variable names and values.
Thanks for quick response mate, i am a total newbie here, what i wanna ask is that i am looking in a tutorial which says. that @PathVariable Map<String,String> pathVars can handle parametrized URI request. So i think you missed what i am trying to achieve.
@SotiriosDelimanolis I know you can assign request parameters and HTTP headers to a map. Does it really work for path variables as well? Do you have a reference to the documentation?
Yes, I was quoting from the javadoc here. The documentation also mentions it here.
0

in some cases if the jspPageName in the ModelAndView(jspPageName) statement

pointing to the page which doesn't exists, you will get the same error

note I use Spring mvc 4.2.4 and JDK 8

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.