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

匿名 (未验证) 提交于 2019-12-03 02:03:01

问题:

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

you need 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



回答2:

Instead of this in your code:

public ModelAndView printBye(@PathVariable Map pathVars) { 

Change it to this:

public ModelAndView printBye(@RequestParam Map 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) { 


回答3:

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:

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



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!