萝卜白菜。 2025-06-26 10:39 采纳率: 100%
浏览 7
已采纳

TongWeb 采用异步servelt报错:Async support must be enabled on a servlet and for all filters involved

TongWeb 采用异步servelt报错:Async support must be enabled on a servlet and for all filters involved in async request processing.

[2025-06-25 18:20:53 226] [INFO] [http-nio-0.0.0.0-8088-exec-2] [systemout] [2025-06-25 18:20:53.226 ERROR 13304 --- [0.0-8088-exec-2] o.s.b.w.servlet.support.ErrorPageFilter  : Forwarding to error page from request [/async] due to exception [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.]
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.
    at org.springframework.util.Assert.state(Assert.java:76) ~[spring-core-5.3.9.jar:5.3.9]
    at org.springframework.web.context.request.async.StandardServletAsyncWebRequest.startAsync(StandardServletAsyncWebRequest.java:112) ~[spring-web-5.3.9.jar:5.3.9]
    at org.springframework.web.context.request.async.WebAsyncManager.startAsyncProcessing(WebAsyncManager.java:483) ~[spring-web-5.3.9.jar:5.3.9]
    at org.springframework.web.context.request.async.WebAsyncManager.startDeferredResultProcessing(WebAsyncManager.java:463) ~[spring-web-5.3.9.jar:5.3.9]
[2025-06-26 09:44:13 789] [SEVERE] [http-nio-0.0.0.0-8088-exec-2] [core] [Servlet.service() for servlet [async0] in context with path [TC_examples] threw exception]
java.lang.IllegalStateException: A filter or servlet of the current chain does not support asynchronous operations.
    at com.tongweb.catalina.connector.Request.startAsync(Request.java:1644)
    at com.tongweb.catalina.connector.Request.startAsync(Request.java:1637)
    at com.tongweb.catalina.connector.RequestFacade.startAsync(RequestFacade.java:1031)
    at async.Async0.service(Async0.java:48)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
  • 写回答

5条回答 默认 最新

  • 阿里嘎多学长 2025-06-26 10:39
    关注

    阿里嘎多学长整理AIGC生成,因移动端显示问题导致当前答案未能完全显示,请使用PC端查看更加详细的解答过程

    异步Servlet报错解决方案

    TongWeb 使用异步Servlet时,可能会出现 "Async support must be enabled on a servlet and for all filters involved" 错误。这是因为在使用异步Servlet时,需要在Servlet和过滤器中启用异步支持。

    解决方案:

    1. 在Servlet中添加 @EnableAsync 注解:
    import org.springframework.web.context.request.async.WebAsyncManager;
    import org.springframework.web.context.request.async.WebAsyncManagerInterceptor;
    
    @ServletComponent
    public class MyServlet extends HttpServlet {
        @Override
        public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            // 你的业务逻辑
        }
        
        @EnableAsync
        public void asyncMethod() {
            // 你的异步业务逻辑
        }
    }
    
    1. 在过滤器中添加 @EnableAsync 注解:
    import org.springframework.web.filter.OncePerRequestFilter;
    import org.springframework.web.context.request.async.WebAsyncManager;
    
    public class MyFilter extends OncePerRequestFilter {
        @Override
        protected void doFilterInternal(HttpServletRequest req, HttpServletResponse resp, FilterChain filterChain) throws ServletException, IOException {
            // 你的过滤器逻辑
        }
        
        @EnableAsync
        public void asyncMethod() {
            // 你的异步过滤器逻辑
        }
    }
    
    1. 在Web.xml文件中添加 async-supported 属性:
    <servlet>
        <servlet-name>MyServlet</servlet-name>
        <servlet-class>com.example.MyServlet</servlet-class>
        <async-supported>true</async-supported>
    </servlet>
    
    1. 在过滤器中添加 async-supported 属性:
    <filter>
        <filter-name>MyFilter</filter-name>
        <filter-class>com.example.MyFilter</filter-class>
        <async-supported>true</async-supported>
    </filter>
    

    通过以上步骤,您可以解决异步Servlet报错问题。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

问题事件

  • 已采纳回答 今天
  • 创建了问题 6月26日