springmvc 开启异步请求报错 Java code using the Servlet API or by adding "true" to servlet and filter declarations in web.xml.

发布时间 2023-08-16 16:42:59作者: 啄木鸟伍迪

报错内容:

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 "true" to servlet and filter declarations in web.xml.

解决方法:

需要在web.xml 的filter 和DispatcherServlet 中配置  

<async-supported>true</async-supported>

官网地址:https://docs.spring.io/spring-framework/reference/web/webmvc/mvc-ann-async.html

如下:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
 3          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd
 4 http://xmlns.jcp.org/xml/ns/javaee " version="4.0">
 5     <filter>
 6         <filter-name>CharacterEncodingFilter</filter-name>
 7         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
 8         <async-supported>true</async-supported>
 9         <init-param>
10             <param-name>encoding</param-name>
11             <param-value>utf-8</param-value>
12         </init-param>
13     </filter>
14     <filter-mapping>
15         <filter-name>CharacterEncodingFilter</filter-name>
16         <url-pattern>/*</url-pattern>
17     </filter-mapping>
18     <context-param>
19         <param-name>contextConfigLocation</param-name>
20         <param-value>classpath:spring.xml,classpath:shiro.xml,classpath:task.xml</param-value>
21     </context-param>
22     <listener>
23         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
24     </listener>
25     <servlet>
26         <servlet-name>springDispatcherServlet</servlet-name>
27         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
28         <async-supported>true</async-supported>
29         <init-param>
30             <param-name>contextConfigLocation</param-name>
31             <param-value>classpath:springmvc.xml</param-value>
32         </init-param>
33         <load-on-startup>1</load-on-startup>
34     </servlet>
35     <servlet-mapping>
36         <servlet-name>springDispatcherServlet</servlet-name>
37         <url-pattern>/</url-pattern>
38     </servlet-mapping>
39     <!-- 404错误 -->
40     <error-page>
41         <error-code>404</error-code>
42         <location>/404.jsp</location>
43     </error-page>
44 
45     <filter>
46         <filter-name>HiddenHttpMethodFilter</filter-name>
47         <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
48         <async-supported>true</async-supported>
49     </filter>
50     <filter-mapping>
51         <filter-name>HiddenHttpMethodFilter</filter-name>
52         <url-pattern>/*</url-pattern>
53     </filter-mapping>
54     <!-- 添加日志监听器 -->
55     <context-param>
56         <param-name>logbackConfigLocation</param-name>
57         <param-value>classpath:logback.xml</param-value>
58     </context-param>
59     <listener>
60         <listener-class>ch.qos.logback.ext.spring.web.LogbackConfigListener</listener-class>
61     </listener>
62     <!-- shiro 配置 start-->
63     <filter>
64         <filter-name>shiroFilter</filter-name>
65         <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
66         <async-supported>true</async-supported>
67         <init-param>
68             <param-name>targetFilterLifecycle</param-name>
69             <param-value>true</param-value>
70         </init-param>
71     </filter>
72 
73     <filter-mapping>
74         <filter-name>shiroFilter</filter-name>
75         <url-pattern>/*</url-pattern>
76     </filter-mapping>
77     <!-- shiro 配置 end-->
78 
79 
80 </web-app>