Spring 4 websocket + Tomcat 7.54 异步支持不工作

标签 spring tomcat spring-mvc servlets spring-websocket

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

服务器信息:Apache Tomcat/7.0.54 小服务版本: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>

dispatcher-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) 

最佳答案

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

<async-supported>true</async-supported>应配置为 <filter> ,也是。

更新

嗯,你的问题很简单:

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

您真的应该为所有请求映射,而不仅仅是

关于Spring 4 websocket + Tomcat 7.54 异步支持不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24903475/

相关文章:

java - 将独立的 Tomcat 实例迁移到 Eclipse

tomcat - IntelliJ 和 Tomcat ....更改的文件不会被 Tomcat 自动识别

java.lang.UnsupportedClassVersionError : myApp : Unsupported major. 次要版本 51.0(无法加载类 myApp)在 Linux 上

java - Spring XML 处理程序映射

java - 在单个查询中获取最后一次插入 mysql 的时间戳以及主键?

java - Spring 或 App-Server escape html 无法正常工作 JAVA MVC

json - Spring Data Rest 不明确关联异常

spring - 具有动态属性的@TestPropertySource

java - 使用 Spring Data Core 中的 PagedResourcesAssembler 进行 HATEOAS 链接生成

spring - 使用 Spring MVC 时无法让 Tomcat 提供一些静态文件