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

I am just new to spring MVC, the following is my code. When I try to navigate to bye

I get the following error

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

      

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

      

+1


source to share


4 answers


you need <mvc:annotation-driven/>

in your configurator-servlet-dispatcher xml

or in more recent spring use

@EnableWebMvc

annotation in your spring config class



and please understand that @EnableWebMvc is created to replace mvc: annotation-driven

and make sure the xmlns are correct in

<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">

      

+7


source


Instead, in your code:

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

      

Change it like this:



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

      

This is because @RequestParam is used to process the Map object passed to the parameter, and @PathVariable is used to process the individual items passed to the parameter. Here's an example @PathVariable:

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

      

+1


source


When matching requests, you specify that your URL has a fixed part /runThis/

and two variable parts {bye}

and {hye}

that you want them to be mapped to two parameters of your method (with matching names as you do not specify anything).

But in your method, you declare a single parameter with a name pathVars

. So you have a mismatch in names, types and number of parameters. This is what Spring MVC is complaining about.

So you want:

@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 easier and safer than your code. And please read the documentation. This is a very simple example.

0


source


in some cases, if jspPageName in ModelAndView expression (jspPageName)

pointing to a page that doesn't exist you will get the same error

note I am using Spring mvc 4.2.4 and JDK 8

0


source







All Articles