我一直在尝试开发具有嵌入式Tomcat的web应用,该应用同时具有@WebServlet
(用于服务网页和RESTful请求)和@ServerEndpoint
(websocket)。到目前为止,在我的Intellij上一切正常。但是,当我使用Maven构建代码并执行jar文件时。我总是遇到以下错误:
由以下原因引起:javax.websocket.DeploymentException:来自服务器的HTTP响应[未找到HTTP / 1.1 404]不允许HTTP升级到WebSocket
该错误可追溯到我尝试打开连接的套接字客户端:
container.connectToServer(this, endpointURI);
供您参考,客户端如下所示:
@ClientEndpoint
public class SocketClient {
Session userSession = null;
private MessageHandler messageHandler;
public SocketClient(URI endpointURI) {
try {
WebSocketContainer container = ContainerProvider.getWebSocketContainer();
container.connectToServer(this, endpointURI);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@OnOpen
public void onOpen(Session userSession) {
System.out.println("opening websocket");
this.userSession = userSession;
}
@OnClose
public void onClose(Session userSession, CloseReason reason) {
System.out.println("closing websocket");
this.userSession = null;
}
@OnMessage
public void onMessage(String message) {
if (this.messageHandler != null) {
this.messageHandler.handleMessage(message);
}
}
public void addMessageHandler(MessageHandler msgHandler) {
this.messageHandler = msgHandler;
}
public void sendMessage(String message) {
this.userSession.getAsyncRemote().sendText(message);
}
public static interface MessageHandler {
public void handleMessage(String message);
}
}
我的websocket端点定义如下:
@ServerEndpoint(
value="/hi",
decoders = MessageDecoder.class,
encoders = MessageEncoder.class )
public class SocketEndpoint {
private Session session;
private static Set<SocketEndpoint> chatEndpoints
= new CopyOnWriteArraySet<>();
public SocketEndpoint() {
System.out.println("class loaded " + this.getClass());
}
@OnOpen
public void onOpen(Session session) {
this.session = session;
chatEndpoints.add(this);
System.out.println(session.getId() + " has open a connection");
try {
session.getBasicRemote().sendText("Connection Established");
} catch (IOException ex) {
ex.printStackTrace();
}
}
@OnMessage
public void onMessage(Message message, Session session) { /* ... */}
@OnClose
public void onClose(Session session) { /* ... */}
@OnError
public void onError(Session session, Throwable throwable) { /* ... */ }
}
就像我说的那样,当我在Intellij上运行代码时,它工作正常,但是有趣的是,当我将鼠标悬停在SocketEndpoint
上时,我会得到一个工具提示,指出该类从未在其他HttpServlet
上使用过他们没有这类问题。我还通过检查SocketEndpoint
类是否已加载(看一下构造函数)进行了验证,而在执行jar时并没有加载该类。
无论需要什么依赖(我可能已经),我都已经添加了它们(pom.xml):
<properties>
<tomcat.version>8.0.28</tomcat.version>
<gson.version>2.8.0</gson.version>
<junit.jupiter.version>5.2.0</junit.jupiter.version>
<junit.platform.version>1.2.0</junit.platform.version>
<junit.vintage.version>5.2.0</junit.vintage.version>
<log4j2.version>2.8.2</log4j2.version>
<maven-surefire-plugin.version>2.21.0</maven-surefire-plugin.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-logging-juli</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jasper</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jasper-el</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jsp-api</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-websocket</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jasper-el</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jsp-api</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-engine</artifactId>
<version>${junit.platform.version}</version>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
<version>${junit.platform.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>${junit.vintage.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-migrationsupport</artifactId>
<version>${junit.vintage.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.websocket</groupId>
<artifactId>javax.websocket-api</artifactId>
<version>1.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.websocket</groupId>
<artifactId>javax.websocket-client-api</artifactId>
<version>1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j2.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple -->
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>${gson.version}</version>
</dependency>
</dependencies>
任何帮助将不胜感激。我看到大多数遇到类似问题的人都在使用Spring,而我没有。
答案 0 :(得分:0)
错误显示为您的客户端尝试连接到未找到的Web套接字(URL)。
Caused by: javax.websocket.DeploymentException: The HTTP response from the server [HTTP/1.1 404 Not Found] did not permit the HTTP upgrade to WebSocket
确保您在自己的内容中提到了任何上下文路径,或者找到了确切的websocket URL。