轉載自 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>