小编典典

调用处理程序之前,带有Tomcat 8和Spring 4 ClassCastException的websockets

tomcat

我试图获得一个简单的Websocket,以将其集成到我的应用程序中。但是,在调用处理程序尝试将Tomcat类型转换为javax对象之前,我无法使其与Spring
API配合使用,并带有ClassCastException。如果我不使用Spring而直接使用Tomcat(因此使用javax.websocket对象和注释),则它运行良好,因此我真的不确定为什么Spring会有问题。

08-May-2014 01:10:12.048 SEVERE [http-nio-8084-exec-88] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [dispatcher] in context with path [/WebsocketSpringTest] threw exception [Request processing failed; nested exception is org.springframework.web.socket.server.HandshakeFailureException: Uncaught failure for request http://localhost:8084/WebsocketSpringTest/socket; nested exception is java.lang.ClassCastException: org.apache.tomcat.websocket.server.WsServerContainer cannot be cast to javax.websocket.server.ServerContainer] with root cause
java.lang.ClassCastException: org.apache.tomcat.websocket.server.WsServerContainer cannot be cast to javax.websocket.server.ServerContainer
at org.springframework.web.socket.server.standard.AbstractStandardUpgradeStrategy.getContainer(AbstractStandardUpgradeStrategy.java:67)

TestSocket.java

import org.springframework.web.socket.WebSocketHandler;
public class TestSocket implements WebSocketHandler {
    ...

web.xml

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

dispatcher-servlet.xml

<context:component-scan base-package="com.willnewbery.websocketspringtest" />
<context:annotation-config />
<mvc:annotation-driven />
<mvc:resources mapping="/resources/**" location="/resources/" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" 
        p:prefix="/WEB-INF/views/" p:suffix=".jsp" />
<websocket:handlers>
    <websocket:mapping path="/socket" handler="socket"/>
</websocket:handlers>
<bean id="socket" class="com.willnewbery.websocketspringtest.TestSocket"/>

pom.xml

<properties>
    <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <spring.version>4.0.4.RELEASE</spring.version>
    <tomcat.version>8.0.3</tomcat.version>
</properties>

<dependencies>
    <!--javax-->
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-web-api</artifactId>
        <version>7.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.websocket</groupId>
        <artifactId>javax.websocket-api</artifactId>
        <version>1.0</version>
    </dependency>
    <!--spring-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-websocket</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <!--tomcat-->
</dependencies>

阅读 337

收藏
2020-06-16

共1个答案

小编典典

可能是“ javax.websocket-
api”依赖项。它已经包含在服务器库中,因此您至少要将其设置为“提供”范围或尝试将其删除,很有可能您不需要在应用程序pom中使用它。

2020-06-16