博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring MVC报异常:org.springframework.web.util.NestedServletException: Request processing failed
阅读量:7041 次
发布时间:2019-06-28

本文共 2502 字,大约阅读时间需要 8 分钟。

 

 

在使用SpringMVC绑定基本类型(如String,Integer等)参数时,应通过@RequestParam注解指定具体的参数名称,否则,当源代码在非debug模式下编译后,运行时会引发HandlerMethodInvocationException异常,这是因为只有在debug模式下编译,其参数名称才存储在编译好的代码中。 

譬如下面的代码会引发异常:

 
@RequestMapping(value = "/security/login", method = RequestMethod.POST)public ModelAndView login(@RequestParam String userName, @RequestParam String password,     HttpServletRequest request) {    ...................... 如果使用Eclipse编译不会在运行时出现异常,这是因为Eclipse默认是采用debug模式编译的,但是如果使用Ant通过javac任务编译的话就会出现异常,除非指定debug=”true”。出现的异常如同:

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.mypackage.security.controller.LoginController.login(java.lang.String,java.lang.String,javax.servlet.http.HttpServletRequest)]; nested exception is java.lang.IllegalStateException: No parameter name specified for argument of type [java.lang.String], and no parameter name information found in class file either. 
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:659) 
.......... 
org.springframework.web.bind.annotation.support.HandlerMethodInvocationException: Failed to invoke handler method [public org.springframework.web.servlet.ModelAndView com.mypackage.security.controller.LoginController.login(java.lang.String,java.lang.String,javax.servlet.http.HttpServletRequest)]; nested exception is java.lang.IllegalStateException: No parameter name specified for argument of type [java.lang.String], and no parameter name information found in class file either. 
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:171) 
.......... 
java.lang.IllegalStateException: No parameter name specified for argument of type [java.lang.String], and no parameter name information found in class file either. 
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.getRequiredParameterName(HandlerMethodInvoker.java:618) 
..........

最好的做法是通过@RequestParam注解指定具体的参数名称,如,上面的代码应该如此编写(注意@RequestParam部分):
Java代码  

@RequestMapping(value = "/security/login", method = RequestMethod.POST)    

public ModelAndView login(@RequestParam("userName") String userName,    @RequestParam("password") String password,   HttpServletRequest request) {    

   ......................  

我喜欢,驾驭着代码在风驰电掣中创造完美!我喜欢,操纵着代码在随必所欲中体验生活!我喜欢,书写着代码在时代浪潮中完成经典!每一段新的代码在我手中诞生对我来说就象观看刹那花开的感动!

转载地址:http://yfhal.baihongyu.com/

你可能感兴趣的文章
webpack 模块热更新 Hot Module Replacement
查看>>
Linux 内核、Shell 简述
查看>>
人脸实时签到(three.js+tracking.js)基于浏览器
查看>>
JavaScript正则表达式
查看>>
面试题 LazyMan 的 Rxjs 实现方式
查看>>
Android Webview打开网页空白
查看>>
记一次tortoisegit access denied错误
查看>>
前端使用 gulp 解决多项目缓存问题
查看>>
Bootstrap基础学习笔记(仅个人学习使用)
查看>>
Android项目实战之高仿网易云音乐项目介绍
查看>>
头条面试题
查看>>
Vue实现一个图片懒加载插件
查看>>
Java开发人员必备Linux命令
查看>>
记一次中台数据传输同步Elasticsearch失败的车祸现场
查看>>
141. Linked List Cycle
查看>>
外部资料集合
查看>>
如何高效读写百万级的Excel?
查看>>
初入门,在vue中使用axios
查看>>
"Sed" 高级功能:我这小脑瓜都快绕晕了
查看>>
另一个go命令行参数处理器 - cmdr
查看>>