Async support must be enabled on a servlet and for all filters involved in async request processing

一、報錯日誌

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.

二、解決辦法
 

1、修改web.xml頭部信息,是因爲<async-supported>true</async-supported>是web.xml 3.0的新特性,所以更改web.xml頭部文件如下即可,如果是跳過:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">

2、添加<async-supported>true</async-supported> 

在web.xml中對DispatcherServlet所有filter添加 :<async-supported>true</async-supported> 

3、如果集成了shiro一定要注意在mapping中增加dispatcher項,否則會拋org.apache.shiro.UnavailableSecurityManagerException異常 

<filter-mapping>
  <filter-name>shiroFilter</filter-name>
  <url-pattern>/*</url-pattern>
  <dispatcher>REQUEST</dispatcher>
  <dispatcher>ASYNC</dispatcher>
</filter-mapping>

4、測試代碼

@RequestMapping("callable")
@ResponseBody
public Callable<String> callable() {
    Callable<String> callable = new Callable<String>() {
        public String call() throws Exception {
            System.out.println("異步開始:" + System.currentTimeMillis());
            Thread.sleep(5000);
            System.out.println("異步結束:" + System.currentTimeMillis());
            return System.currentTimeMillis() +"";
        }
    };
    System.out.println("主線程開始:" + System.currentTimeMillis());
    return callable;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章