使用 Servlet 3.0 的 Async DeferredResult 长轮询 IllegalStateException 错误 Tomcat 7

Async DeferredResult Long Polling IllegalStateException Error by using Servlet 3.0 Tomcat 7

我在 Web 应用程序中使用 Async DeferredResult 长轮询时遇到问题。

具体来说,我收到一个错误“java.lang.IllegalStateException:必须在 servlet 上以及异步请求处理中涉及的所有过滤器上启用异步支持。这在 Java 代码使用 Servlet API 或通过将 "true" 添加到 web.xml."

中的 servlet 和过滤器声明

当我使用 Ajax 调用我的控制器 (@Controller) 的一种方法时产生此错误。

我的应用程序是使用以下设置的:

我已遵循有关 web.xml 的所有相关说明并使用标签

完成所有 servlet 和过滤器
<async-supported>true</async-supported>

您可以在下面找到所有 web.xml

<?xml version="1.0" encoding="UTF-8"?>

http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 版本="3.0">

<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        classpath:net/bull/javamelody/monitoring-spring.xml
        /WEB-INF/spring/root-context.xml
        /WEB-INF/spring/menu-structure.xml
    </param-value>
</context-param>


<!-- Java melody filter mapping and listener -->
<filter>
    <filter-name>monitoring</filter-name>
    <filter-class>net.bull.javamelody.MonitoringFilter</filter-class>
    <async-supported>true</async-supported>
</filter>
<filter-mapping>
    <filter-name>monitoring</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
    <listener-class>net.bull.javamelody.SessionListener</listener-class>
</listener>


<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- Processes application requests -->
<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    <async-supported>true</async-supported>
</servlet>

<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<!-- Dandelion-Datatables servlet definition -->
<servlet>
    <servlet-name>datatablesController</servlet-name>
    <servlet-class>com.github.dandelion.datatables.extras.servlet2.servlet.DatatablesServlet</servlet-class>
    <async-supported>true</async-supported>
</servlet>

<!-- Dandelion-Datatables servlet mapping -->
<servlet-mapping>
    <servlet-name>datatablesController</servlet-name>
    <url-pattern>/datatablesController/*</url-pattern>
</servlet-mapping>

有什么建议吗?

提前致谢。

我发现了导致这种行为的原因。

问题主要出在datatablesController servlet 上,该servlet 是指版本0.9.2。具体来说,在我的 pom.xml 中有:

<dependency>
    <groupId>com.github.dandelion</groupId>
    <artifactId>datatables-jsp</artifactId>
    <version>0.9.2</version>
</dependency>
<dependency>
    <groupId>com.github.dandelion</groupId>
    <artifactId>datatables-servlet2</artifactId>
    <version>0.9.2</version>
</dependency>

所以为了解决它,我将这些依赖项更改为:

<dependency>
    <groupId>com.github.dandelion</groupId>
    <artifactId>datatables-jsp</artifactId>
    <version>0.10.1</version>
</dependency>

并且我将 web.xml 中的 datatablesController servlet 更改为:

 <!-- Dandelion filter definition and mapping -->
<filter>
    <filter-name>dandelionFilter</filter-name>
    <filter-class>com.github.dandelion.core.web.DandelionFilter</filter-class>
    <async-supported>true</async-supported>
</filter>
<filter-mapping>
    <filter-name>dandelionFilter</filter-name>
    <url-pattern>/</url-pattern>
</filter-mapping>


<!-- Dandelion servlet definition and mapping -->
<servlet>
    <servlet-name>dandelionServlet</servlet-name>
    <servlet-class>com.github.dandelion.core.web.DandelionServlet</servlet-class>
    <async-supported>true</async-supported>
</servlet>
<servlet-mapping>
    <servlet-name>dandelionServlet</servlet-name>
    <url-pattern>/dandelion-assets/</url-pattern>
</servlet-mapping>

一切如预期。

基本上每个 <servlet><filter> 标签都应该包含:

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

它适用于 me.Check 下面的代码:-

<servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>WEB-INF/mvc-dispatcher.xml</param-value>
        </init-param>

        <load-on-startup>1</load-on-startup>
        <async-supported>true</async-supported>
    </servlet>
    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>


<!-- Spring Security -->
    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
        <async-supported>true</async-supported>
    </filter>

    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>