Websocket 应用程序在嵌入的 glassfish 中没有 运行,但在独立版本中有 运行
Websocket application doesnt run in glassfish embedded, but runs in standalone version
晚上好,
我有一个简单的 websocket 应用程序。如果我使用 asadmin 命令或我的 IDE 将它部署到独立的 glassfish 服务器,它运行良好。当我使用 glassfish-embedded 时,一切似乎都很好(没有错误消息),但我在应该为 websockets 服务的端点得到 404。
WebSocket connection to 'ws://localhost:8080/test/websocket/zelitomasfds/efdsdfsdf' failed: Error during WebSocket handshake: Unexpected response code: 404
这是代码,应该很简单
/* Imports skipped */
@ServerEndpoint("/websocket/{email}/{type}")
public class SimpleHOTPWebSocket {
@OnMessage
public void onMessage(String message, Session session)
throws IOException, InterruptedException {
System.out.println("Received: " + message);
}
@OnOpen
public void onOpen(Session session,
EndpointConfig c,
@PathParam("email") String email,
@PathParam("type") String type) {
System.out.println("Client " + email + " connected as a " + type);
}
@OnClose
public void onClose(Session session) {
System.out.println("Connection closed");
}
}
这是我的 pom.xml:
我也尝试过 运行 使用 webapp-runner 和 jetty,所以问题一定出在我的代码中或 pom.xml.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cf.zelitomas</groupId>
<artifactId>simplesocket</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>simplesocket</name>
<pluginRepositories>
<pluginRepository>
<id>m.g.o-groups-glassfish</id>
<url>http://maven.glassfish.org/content/groups/glassfish</url>
</pluginRepository>
</pluginRepositories>
<properties>
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.extras</groupId>
<artifactId>glassfish-embedded-all</artifactId>
<version>3.1.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<compilerArguments>
<endorseddirs>${endorsed.dir}</endorseddirs>
</compilerArguments>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<outputDirectory>${endorsed.dir}</outputDirectory>
<silent>true</silent>
<artifactItems>
<artifactItem>
<groupId>javax</groupId>
<artifactId>javaee-endorsed-api</artifactId>
<version>7.0</version>
<type>jar</type>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.glassfish</groupId>
<artifactId>maven-embedded-glassfish-plugin</artifactId>
<version>3.0.1</version>
<configuration>
<app>${project.build.directory}/${build.finalName}.war</app>
<autoDelete>true</autoDelete>
<port>8080</port>
<contextRoot>test</contextRoot>
</configuration>
</plugin>
</plugins>
</build>
</project>
我正在用 mvn clean install embedded-glassfish:run
启动服务器
如果您想查看源代码或尝试 war 文件,请随时在此处下载 (war, source)
拜托,谁能看出我做错了什么?
感谢您的帮助,我现在已经为此苦苦挣扎了 10 多个小时。
也很抱歉我的英语不好
你在标题中提到了 glassfish-embedded,但你也提到你尝试使用码头和 webapp-runner,所以我知道在 "embedded" 模式下你没有关心你用的是哪个。
所以我下载了您的源代码,进行了尝试,重现了问题(同样的行为),然后...删除了 POM 中的几乎所有内容并从头开始添加了所需的位,使用 Jetty 因为我更熟悉它,它只需要中央 maven 存储库(没有添加 repo)。总体来说简单了很多。
您的代码只需要一个依赖项:javax.websocket-api
。剩下的就是 jetty:run
的配置(我配置了上下文路径“/test”以满足您的期望,默认为“/”)。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cf.zelitomas</groupId>
<artifactId>simplesocket</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>simplesocket</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jetty-version>9.4.6.v20170531</jetty-version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>${jetty-version}</version>
<configuration>
<webApp>
<contextPath>/test</contextPath>
</webApp>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>javax.websocket</groupId>
<artifactId>javax.websocket-api</artifactId>
<version>1.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
简而言之:用上面的替换你的POM,执行mvn jetty:run
,然后访问http://localhost:8080/test
。
晚上好,
我有一个简单的 websocket 应用程序。如果我使用 asadmin 命令或我的 IDE 将它部署到独立的 glassfish 服务器,它运行良好。当我使用 glassfish-embedded 时,一切似乎都很好(没有错误消息),但我在应该为 websockets 服务的端点得到 404。
WebSocket connection to 'ws://localhost:8080/test/websocket/zelitomasfds/efdsdfsdf' failed: Error during WebSocket handshake: Unexpected response code: 404
这是代码,应该很简单
/* Imports skipped */
@ServerEndpoint("/websocket/{email}/{type}")
public class SimpleHOTPWebSocket {
@OnMessage
public void onMessage(String message, Session session)
throws IOException, InterruptedException {
System.out.println("Received: " + message);
}
@OnOpen
public void onOpen(Session session,
EndpointConfig c,
@PathParam("email") String email,
@PathParam("type") String type) {
System.out.println("Client " + email + " connected as a " + type);
}
@OnClose
public void onClose(Session session) {
System.out.println("Connection closed");
}
}
这是我的 pom.xml:
我也尝试过 运行 使用 webapp-runner 和 jetty,所以问题一定出在我的代码中或 pom.xml.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cf.zelitomas</groupId>
<artifactId>simplesocket</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>simplesocket</name>
<pluginRepositories>
<pluginRepository>
<id>m.g.o-groups-glassfish</id>
<url>http://maven.glassfish.org/content/groups/glassfish</url>
</pluginRepository>
</pluginRepositories>
<properties>
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.extras</groupId>
<artifactId>glassfish-embedded-all</artifactId>
<version>3.1.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<compilerArguments>
<endorseddirs>${endorsed.dir}</endorseddirs>
</compilerArguments>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<outputDirectory>${endorsed.dir}</outputDirectory>
<silent>true</silent>
<artifactItems>
<artifactItem>
<groupId>javax</groupId>
<artifactId>javaee-endorsed-api</artifactId>
<version>7.0</version>
<type>jar</type>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.glassfish</groupId>
<artifactId>maven-embedded-glassfish-plugin</artifactId>
<version>3.0.1</version>
<configuration>
<app>${project.build.directory}/${build.finalName}.war</app>
<autoDelete>true</autoDelete>
<port>8080</port>
<contextRoot>test</contextRoot>
</configuration>
</plugin>
</plugins>
</build>
</project>
我正在用 mvn clean install embedded-glassfish:run
如果您想查看源代码或尝试 war 文件,请随时在此处下载 (war, source) 拜托,谁能看出我做错了什么?
感谢您的帮助,我现在已经为此苦苦挣扎了 10 多个小时。
也很抱歉我的英语不好
你在标题中提到了 glassfish-embedded,但你也提到你尝试使用码头和 webapp-runner,所以我知道在 "embedded" 模式下你没有关心你用的是哪个。
所以我下载了您的源代码,进行了尝试,重现了问题(同样的行为),然后...删除了 POM 中的几乎所有内容并从头开始添加了所需的位,使用 Jetty 因为我更熟悉它,它只需要中央 maven 存储库(没有添加 repo)。总体来说简单了很多。
您的代码只需要一个依赖项:javax.websocket-api
。剩下的就是 jetty:run
的配置(我配置了上下文路径“/test”以满足您的期望,默认为“/”)。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cf.zelitomas</groupId>
<artifactId>simplesocket</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>simplesocket</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jetty-version>9.4.6.v20170531</jetty-version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>${jetty-version}</version>
<configuration>
<webApp>
<contextPath>/test</contextPath>
</webApp>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>javax.websocket</groupId>
<artifactId>javax.websocket-api</artifactId>
<version>1.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
简而言之:用上面的替换你的POM,执行mvn jetty:run
,然后访问http://localhost:8080/test
。