晚上好,
我有一个简单的websocket应用程序。如果我使用asadmin命令或我的IDE将其部署到独立的glassfish服务器,它运行正常。当我使用glassfish- 嵌入式时,一切似乎都很好(没有错误消息),但我在端点获得404应该服务websockets。
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个多小时。
也很抱歉我的英文不好
答案 0 :(得分:1)
You mention glassfish-embedded in title, but you also mention you tried with jetty and webapp-runner, so I understand that in "embedded" mode you don't care about which you use.
So I downloaded your sources, tried it, reproduced the issue (same behavior), and... removed almost everything from the POM and added the required bits from scratch, using Jetty because I'm more familiar with it, and it only needs central maven repository (no repo added). Overall it's a lot simpler.
Your code only needs a single dependency: javax.websocket-api
. All the rest is configuration for jetty:run
(which I configured with context path "/test" to match your expectations, as default is "/").
<?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>
In short: replace your POM with the above, execute mvn jetty:run
, and visit http://localhost:8080/test
.