我在Spring MVC的控制器类中编写了一个请求处理程序方法。当我调用http://localhost:8080/ProjectName/hello/santhosh/india
网址时,我收到了HTTP状态500 - 异常。
这是方法:
@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;
}
这是一个例外:
HTTP状态500 -
输入例外报告
消息
description服务器遇到阻止的内部错误() 它来自履行这一要求。
例外
org.springframework.web.util.NestedServletException:Request 处理失败;嵌套异常是 org.springframework.web.bind.annotation.support.HandlerMethodInvocationException: 无法调用处理程序方法[public org.springframework.web.servlet.ModelAndView com.techmahindra.controller.LoginController.sayHello(java.util.Map)]; 嵌套异常是java.lang.IllegalStateException:找不到 @RequestMapping中的@PathVariable [pathVar] 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)
根本原因
org.springframework.web.bind.annotation.support.HandlerMethodInvocationException: 无法调用处理程序方法[public org.springframework.web.servlet.ModelAndView com.techmahindra.controller.LoginController.sayHello(java.util.Map)]; 嵌套异常是java.lang.IllegalStateException:找不到 @RequestMapping中的@PathVariable [pathVar] 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)
根本原因
java.lang.IllegalStateException:找不到@PathVariable @ strongquest_apping中的 [pathVar] 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)
答案 0 :(得分:0)
我从这个链接找到了答案。 Could not find @PathVariable [pathVars] in @RequestMapping Spring MVC
我需要在我的dispatcher-servlet.xml
中添加<mvc:annotation-driven />