转载自 http://panyongzheng.iteye.com/blog/2222666 谢谢 保留收藏
关于spring java.lang.IllegalArgumentException: Name for argument type [java.lang.String] 的错误 http://blog.csdn.net/liuguanghaoputin/article/details/8695600
Name for argument type [java.lang.String] not available http://m.blog.csdn.net/blog/kouwoo/42869779
SpringMVC强大的数据绑定(2)——第六章 注解式控制器详解——跟着开涛学SpringMVC http://jinnianshilongnian.iteye.com/blog/1705701
为了这个问题费了很大劲,主要参考了了
1.http://stackoverflow.com/questions/2622018/compile-classfile-issue-in-spring-3 这个主要是因为ant编译导致类似的问题。
2. http://stackoverflow.com/questions/10305592/error-class-names-are-only-accepted-if-annotation-processing-is-explicitly-req 一开始src的写法有些问题,改为上面的写法便可。
发现问题:
一次做下载过程中,使用get方式进行下载,
- @ResponseBody
- @RequestMapping(value = "/down/{_listid}/{type}", method = RequestMethod.GET)
- public ResponseEntity<byte[]> download(@PathVariable String _listid, @PathVariable String type) throws IOException {
- ......
- }
在idea和eclipse下面启动工程没问题, 但是部署的时候就出了问题,后台一直提示500错误,但看不到错误细节,后来我直接复制地址,打开标签,直接粘贴url,才看到下面错误:
Error 500: org.springframework.web.util.NestedServletException:
Request processing failed; nested exception is java.lang.IllegalArgumentException:
Name for argument type [java.lang.String] not available, and parameter name information not found in class file either.
原因:
这个错误主要是因为action的参数标注默认是debug级别,比如
- @RequestMapping(value = "/security/login", method = RequestMethod.POST)
- public ModelAndView login(@RequestParam String userName, @RequestParam String password,
- HttpServletRequest request) {
此时userName的级别时debug级别,而在linux下编译时是忽略了这些标注,导致请求时就会找不到userName的参数。eclipse默认是debug级别的函数里面的参数名保留,但是ant编译就不行了。
解决方法1:写全@RequestParam的参数
- @RequestMapping("hello")
- public String helloWorld(Map<String, Object> map, HttpServletRequest request,@RequestParam(value="hhh", required = false) String hhh) {
- System.out.println("hello");
- System.out.println("["+hhh+"]");
- map.put("message", "test message111");
- return "helloView";
- }
写全@PathVariable的参数
- @RequestMapping(value="/users/{userId}/topics/{topicId}")
- public String test(
- @PathVariable(value="userId") int userId,
- @PathVariable(value="topicId") int topicId)
解决方法2:修改build.xml,使用javac debug=true
- <javac srcdir="${src}" destdir="${build}/WEB-INF/classes" debug="true" encoding="utf-8" classpathref="classpath" includeantruntime="on">
- </javac>