-4

I have written a request handler method in a controller class in Spring MVC. When I'm calling the http://localhost:8080/ProjectName/hello/santhosh/india URL, I got a HTTP Status 500 - exception.

This is the method:

@RequestMapping("/hello/{userName}/{country}")
public ModelAndView sayHello(@PathVariable Map<String, String> pathVar){
    String name = pathVar.get("userName");
    String country = pathVar.get("country");
    System.out.println("Hello!!" + name + " from " + country);

    ModelAndView model = new ModelAndView("login");
    model.addObject("msg","Hello!!" + name);
    return model;
}

This is the exception:

HTTP Status 500 -

type Exception report

message

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.techmahindra.controller.LoginController.sayHello(java.util.Map)]; nested exception is java.lang.IllegalStateException: Could not find @PathVariable [pathVar] in @RequestMapping org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:982) org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861) javax.servlet.http.HttpServlet.service(HttpServlet.java:617) org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

root cause

org.springframework.web.bind.annotation.support.HandlerMethodInvocationException: Failed to invoke handler method [public org.springframework.web.servlet.ModelAndView com.techmahindra.controller.LoginController.sayHello(java.util.Map)]; nested exception is java.lang.IllegalStateException: Could not find @PathVariable [pathVar] in @RequestMapping org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:185) org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:440) org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:428) org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963) org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897) org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861) javax.servlet.http.HttpServlet.service(HttpServlet.java:617) org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

root cause

java.lang.IllegalStateException: Could not find @PathVariable [pathVar] in @RequestMapping org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter$ServletHandlerMethodInvoker.resolvePathVariable(AnnotationMethodHandlerAdapter.java:856) org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolvePathVariable(HandlerMethodInvoker.java:718) org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:367) org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:175) org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:440) org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:428) org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963) org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897) org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861) javax.servlet.http.HttpServlet.service(HttpServlet.java:617) org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

3
  • docs.spring.io/spring/docs/current/spring-framework-reference/… Commented Aug 31, 2016 at 22:07
  • Which part of "Could not find @PathVariable [pathVar] in @RequestMapping" is confusing you, when the path /hello/{userName}/{country} only defines the variables userName and country? Change method to sayHello(@PathVariable String userName, @PathVariable String country). Commented Aug 31, 2016 at 22:12
  • Which version of Spring are you using? I tested your method in 3.2.11.RELEASE version of Spring project I have and the mapping of path variables into the map worked fine. Commented Aug 31, 2016 at 22:15

1 Answer 1

0

I have found the answer from this link. Could not find @PathVariable [pathVars] in @RequestMapping Spring MVC

I need to add <mvc:annotation-driven /> in my dispatcher-servlet.xml

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.