Springmvc非同步支援報錯
阿新 • • 發佈:2019-01-30
參看網上的springmvc非同步支援的小例子,自己試了一下,使用ajax的請求處理方法如下:
@RequestMapping("/response-body") public @ResponseBody Callable<String> responseBody(){ return new Callable<String>() { @Override public String call() throws Exception { Thread.sleep(2000); return "Callable result"; } }; }
但是在呼叫時出錯:
Servlet.service() for servlet [springmvc] in context with path [/SpringMVC_Demo] threw exception [Request processing failed; nested exception is java.lang.IllegalStateException: Async support must be enabled on a servlet and for all filters involved in async request processing. This is done in Java code using the Servlet API or by adding "<async-supported>true</async-supported>" to servlet and filter declarations in web.xml.] with root cause java.lang.IllegalStateException: Async support must be enabled on a servlet and for all filters involved in async request processing. This is done in Java code using the Servlet API or by adding "<async-supported>true</async-supported>" to servlet and filter declarations in web.xml.
但是自己也確實在web.xml中加入了非同步支援
仔細一看,報錯提示有“filter declarations“ ,難道filter也要修改一下?<servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> <!-- springmvc的非同步支援 --> <async-supported>true</async-supported> </servlet>
我的專案是用的以前練習時的專案,其中使用了springmvc的encoding-filter,為了驗證猜想,將filter暫時註釋掉,結果沒再出錯,說明對filter的使用也要加以非同步支援宣告。
現在在servlet和filter中也加入非同步支援,即在每個filter中加入<async-supported>true</async-supported>,再次重啟,訪問,也不會報錯了。
參考:http://www.blogjava.net/yongboy/archive/2011/01/15/346203.html
http://wiselyman.iteye.com/blog/2215852
之後補充:專案中如果引入activemq-all.jar的包,進行springmvc的非同步訪問是會報錯的。
NoSuchMethodError: org.springframework.core.MethodParameter.getContainingClass()Ljava/lang/Class;
網上說是jar包衝突,更換activemq的版本即可,此點未測試