5

我正在使用 Spring websockets 和 stomp.js 创建一个示例聊天应用程序,我使用的是 tomcat 7.54,但是在运行该应用程序时,当浏览器发出 xhr 请求时,我遇到了一个支持异步的错误。

服务器信息:Apache Tomcat/7.0.54 Servlet 版本:3.0 JSP 版本:2.2 Java 版本:1.7.0_25

Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<servlet>
    <async-supported>true</async-supported>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
<session-config>
    <session-timeout>
        30
    </session-timeout>
</session-config>

调度程序-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:websocket="http://www.springframework.org/schema/websocket"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
        http://www.springframework.org/schema/websocket http://www.springframework.org/schema/websocket/spring-websocket-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>

    <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="index.htm">indexController</prop>
            </props>
        </property>
    </bean>

    <context:component-scan base-package="hello" />

    <websocket:message-broker application-destination-prefix="/app">
        <websocket:stomp-endpoint path="/hello">
            <websocket:sockjs/>
        </websocket:stomp-endpoint>
        <websocket:simple-broker prefix="/topic"/>
    </websocket:message-broker>

    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/jsp/"
          p:suffix=".jsp" />

    <bean name="indexController"
          class="org.springframework.web.servlet.mvc.ParameterizableViewController"
          p:viewName="index" />

</beans>

错误

java.lang.IllegalArgumentException: 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. Also you must use a Servlet 3.0+ container
    at org.springframework.util.Assert.isTrue(Assert.java:65) 
4

6 回答 6

7

我猜你没有显示整个web.xml.

<async-supported>true</async-supported>也应该配置为<filter>

更新

好吧,你的问题很简单:

<servlet-name>dispatcher</servlet-name>
<url-pattern>/*</url-pattern>

您确实应该映射所有请求,而不仅仅是root

于 2014-07-23T06:53:18.657 回答
3

<async-supported>true</async-supported>应该包含在 servlet 和 filter 标记中。使用以下代码段作为参考:

<web-app ...>
    ...
    <servlet>
        <servlet-name>instantaction</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                classpath:META-INF/spring/web/my-servlet-config.xml
            </param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
        <async-supported>true</async-supported>
    </servlet>

    <servlet-mapping>
        ...
    </servlet-mapping>

    <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>
</web-app>
于 2015-11-26T10:47:56.817 回答
1

如果您使用反向代理,请在 nginx.conf 文件中添加以下内容。

  # For WebSocket upgrade header
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
于 2018-06-26T13:15:51.103 回答
0

确保没有其他注入的组件禁用异步支持。


细节

我了解到 Spring 默认提供异步支持。

并且(远程相关的)同步日志配置可能会禁用整个服务的异步处理。

具体来说,我的 logback 集成缺少IMPORTANT行:

public EmbeddedServletContainerCustomizer containerCustomizer(
    final String logbackAccessClasspathConfig
) {
    return container -> {
        if (container instanceof TomcatEmbeddedServletContainerFactory) {
            ((TomcatEmbeddedServletContainerFactory) container)
                .addContextCustomizers(context -> {
                    LogbackValve logbackValve = new LogbackValve();
                    logbackValve.setFilename(logbackAccessClasspathConfig);

                    // IMPORTANT:
                    logbackValve.setAsyncSupported(true);

                    context.getPipeline().addValve(logbackValve);
                }
            );
        }
    };
}

感谢其他答案:

于 2019-08-21T11:56:20.967 回答
0

尝试升级你的 jdk 和 tomcat 版本。我也遇到了这个问题,我把jdk从1.7升级到1.8,tomcat从7.0.54升级到7.0.75,解决了这个问题。

于 2017-10-17T01:27:16.280 回答
0

我在使用 SpringBoot 应用程序时遇到了同样的问题,对我来说,它开始工作了

1)添加asyncSupported到过滤器后@WebFilter(urlPatterns="/api-acess/*",asyncSupported = true )

2)增加了sameOrigin()WebSecurityConfigurerAdapter扩展类的支持,比如http.headers().frameOptions().sameOrigin();

我希望它会帮助别人。

于 2019-11-26T12:03:02.803 回答