我正在尝试使用一个简单的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>
调度-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>
答案 0 :(得分:6)
“javax.websocket-api”依赖关系可能是个问题。它已经包含在服务器库中,因此您至少要使其“提供”范围或尝试删除它,您很可能在应用程序pom中不需要它。
答案 1 :(得分:4)
从spring-boot-starter-web;
中排除tomcat-embed-websocket依赖项 <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<artifactId>tomcat-embed-logging-juli</artifactId>
<groupId>org.apache.tomcat.embed</groupId>
</exclusion>
<exclusion>
<artifactId>tomcat-embed-websocket</artifactId>
<groupId>org.apache.tomcat.embed</groupId>
</exclusion>
<exclusion>
<artifactId>tomcat-embed-el</artifactId>
<groupId>org.apache.tomcat.embed</groupId>
</exclusion>
</exclusions>
</dependency>
答案 2 :(得分:0)
您需要从war文件中排除tomcat-embed-websocket - * .jar。
将此行添加到build.gradle
providedCompile("org.apache.tomcat.embed:tomcat-embed-websocket")
或在Maven:
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-websocket</artifactId>
<version>${tomcat.version}</version>
</dependency>
答案 3 :(得分:0)
在使用Spring Boot构建WAR文件时,参考文档将指出您需要将Tomcat转换为提供的依赖项。可以在参考文档中看到Maven和Gradle的推荐做法here =&gt; http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-create-a-deployable-war-file