简单的 Maven 项目,带有用于测试 Websocket 的主要方法。
使用
main
运行 mvn exec:java
方法会抛出 "Could not find an implementation class."
。
public class TestMain {
public static void main(String[] args) throws IOException, InterruptedException {
String endpoint = "wss://toto.tutu.com";
String wssEndpointUri = "/mywebsocketEndpoint"
WebSocketContainer container =
ContainerProvider.getWebSocketContainer();
try {
container.connectToServer(CustomWebsocketListener.class,
new URI(endpoint.concat(wssEndpointUri)));
} catch (DeploymentException | IOException | URISyntaxException ex) {
ex.printStackTrace();
}
}
}
POM:
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>javax.websocket</groupId>
<artifactId>javax.websocket-api</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.tyrus.bundles</groupId>
<artifactId>tyrus-standalone-client</artifactId>
<version>2.1.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>com.test.TestMain</mainClass>
</configuration>
</plugin>
</plugins>
</build>
我做到了
mvn install
。
我已读过:
如何将WebSocketContainer实现到StandardWebSocketClient类中
Tyrus - 简单的 java 应用程序 - 找不到实现类
应该检测 JSR-356 实现,除非我使用了错误的依赖项或者依赖项中的 META-INF 声明设置错误?不知道与上面的读数有什么不同。
感谢您的回答。
https://stackoverflow.com/a/75795507/5525384
Tyrus 客户端 1.x -> javax.websocket 1.1
Tyrus 客户端 2.x -> 雅加达 websockets