问题描述
我正在尝试使用一个简单的websocket来集成到我的应用程序中。但是我无法使用Spring API,在调用我的处理程序尝试将Tomcat类型转换为javax对象之前的某个地方使用ClassCastException。如果我只是在没有Spring的情况下直接使用Tomcat(那么javax.websocket对象和注释)它可以很好地工作,所以我真的不确定为什么Spring存在问题。
I am trying to get a simple websocket to work to integrate into my application. However I have not been able to get it to work with the Spring API's, with a ClassCastException somewhere before my handler is invoked trying to convert a Tomcat type to a javax object. If I just use Tomcat directly without Spring (so the javax.websocket objects and annotations) it works perfectly, so I am really not sure why there is an issue with 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
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
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>
推荐答案
javax.websocket-api依赖关系可能是问题。它已经包含在服务器库中,因此您至少将其范围提供或尝试删除它,您很可能在应用程序pom中不需要它。
The "javax.websocket-api" dependency may be the issue. It is already included in the server lib so you at least make it scope "provided" or try removing it, there is a good chance you don't need it in the application pom.
这篇关于调用处理程序之前使用Tomcat 8和Spring 4 ClassCastException的websockets的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!