Java EJB Websocket握手错误:响应代码不是101:404

Java EJB Websocket握手错误:响应代码不是101:404,java,websocket,jboss,ejb,java-websocket,Java,Websocket,Jboss,Ejb,Java Websocket,如果您没有时间阅读详细信息,我的问题是: 当一个简单的websocket请求发送到EJB模块中的ejbwebsocket端点服务器时,我得到Http错误代码404 (由maven使用java8、EJB3和JBOSS EAP 7.2创建的项目) 请注意,ejb模块既不能有web.xml也不能有maven contextRoot标记 细节: 我用EJB3开发了一个企业项目,并使用IntellijIdea中的maven通过JBOSS-EAP7.2进行了部署。 我需要提供一个websocket服务器和客

如果您没有时间阅读详细信息,我的问题是:

当一个简单的websocket请求发送到EJB模块中的ejbwebsocket端点服务器时,我得到Http错误代码404 (由maven使用java8、EJB3和JBOSS EAP 7.2创建的项目) 请注意,ejb模块既不能有
web.xml
也不能有
maven contextRoot标记

细节: 我用EJB3开发了一个企业项目,并使用IntellijIdea中的maven通过JBOSS-EAP7.2进行了部署。 我需要提供一个websocket服务器和客户端来与第三方通信。提供的一些代码如下所示:

public class HelloEndpoint extends Endpoint {
    private Session session;

    @Override
    public void onOpen(Session session, EndpointConfig endpointConfig) {
        this.session = session;

        this.session.addMessageHandler(new MessageHandler.Whole<String>() {
            @Override
            public void onMessage(String message) {
                System.out.println("!!!!!!  retrieved message: " + message);
            }
        });
    }

    public void sendMessage(String message){
        try {
            this.session.getBasicRemote().sendText(message);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
服务器端:

@ServerEndpoint("/chat")
public class ChatServer {
    private Session session;

    @OnOpen
    public void connect(Session session){
        this.session=session;
    }

    @OnClose
    public void close(){
        this.session=null;
    }
    
    @OnMessage
    public void processMessage(String msg){
        System.out.println("msg = " + msg);;
        if(this.session!=null && this.session.isOpen()){
            try {
                this.session.getBasicRemote().sendText("this is from server");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
@Provider
@PreMatching
public class SecurityFilter implements ContainerRequestFilter, ContainerResponseFilter {
    @Override
    public void filter(ContainerRequestContext containerRequestContext) throws IOException {
    //some snipet to evaluate the JAX-RS and nothing to do with websocket requests
    //websocket request received here but it did not go toward EndpointServer
    }

    @Override
    public void filter(ContainerRequestContext containerRequestContext, ContainerResponseContext containerResponseContext) throws IOException {
        System.out.println("response status: "+ containerResponseContext.getStatus());
        //here I get Http status code 404 
    }
}
public class SocketSmokeTest {
    private WebSocketContainer container;
    private HelloEndpoint endpoint;

    @Test
    public void communicate() {
        try {
            Session connectToServer = container.connectToServer
            (this.endpoint, new URI("ws://127.0.0.1:8080/gateway/chat"));
            //I have tried other path like "ws://127.0.0.1:8080/rootsource/gateway/chat"
            //or "ws://127.0.0.1:8080/rootsource/chat"
            this.endpoint.sendMessage("hello from client");
            Thread.sleep(3000);
        } catch (URISyntaxException e) {
            e.printStackTrace();
        } catch (DeploymentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
GET /resources/websocket/chatserver/ HTTP/1.1
Connection: Upgrade
Host: 127.0.0.1:8080
Origin: http://127.0.0.1:8080
Sec-WebSocket-Key: K3mTSnyiVbi5/yeyIwFR2Q==
Sec-WebSocket-Version: 13
Upgrade: websocket

HTTP/1.1 404 Not Found
Connection: keep-alive
Content-Length: 0
Date: Sat, 05 Sep 2020 05:02:43 GMT
...
<dependencies>
    ...
</dependencies>
<build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-ear-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <defaultLibBundleDir>lib</defaultLibBundleDir>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                        </manifest>
                    </archive>
                    <modules>
                        <ejbModule>
                            <groupId>ir.co.isc</groupId>
                            <artifactId>BusinessLayer</artifactId>
                        </ejbModule>
                        <ejbModule>
                            <groupId>ir.co.isc</groupId>
                            <artifactId>DataAccessLayer</artifactId>
                        </ejbModule>
                        <ejbModule>
                            <groupId>ir.co.isc</groupId>
                            <artifactId>PresentationLayerGateway</artifactId>
                        </ejbModule>
                        <webModule>
                            <groupId>ir.co.isc</groupId>
                            <artifactId>PresentationLayerGUI</artifactId>
                            <contextRoot>/rootsource</contextRoot>
                        </webModule>
                    </modules>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-ejb-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
            </plugin>
        </plugins>
    </pluginManagement>
</build>
...
<?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">
    <parent>
        <artifactId>Azmoonyar</artifactId>
        <groupId>ir.co.isc</groupId>
        <version>1.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>PresentationLayerGateway</artifactId>
    <packaging>ejb</packaging>

    <name>PresentationLayerGateway</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>ir.co.isc</groupId>
            <artifactId>BusinessLayer</artifactId>
            <version>1.0</version>
            <type>ejb</type>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>8.0.1</version>
            <scope>provided</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/javax.ejb/ejb-api -->
        <dependency>
            <groupId>javax.ejb</groupId>
            <artifactId>ejb-api</artifactId>
            <version>3.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.websocket</groupId>
            <artifactId>javax.websocket-api</artifactId>
            <version>1.1</version>
        </dependency>
        <dependency>
            <groupId>javax.websocket</groupId>
            <artifactId>javax.websocket-client-api</artifactId>
            <version>1.1</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.tyrus</groupId>
            <artifactId>tyrus-client</artifactId>
            <version>1.17</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.glassfish.tyrus.bundles</groupId>
            <artifactId>tyrus-standalone-client</artifactId>
            <version>1.17</version>
        </dependency>
    </dependencies>

    <build>
        <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-ejb-plugin</artifactId>
                    <version>3.1.0</version>
                    <configuration>
                        <ejbVersion>3.0</ejbVersion>
                        <archive>
                            <manifest>
                                <addClasspath>true</addClasspath>
                            </manifest>
                        </archive>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.1</version   >
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>3.0.0-M3</version>
                    <configuration>
                        <skipTests>${skipTests}</skipTests>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>
在服务器上,我们还有这个类来声明根路径(并启用JAX-RS):

此外,我在服务器端有一个过滤器,如下所示,它对websocket请求不做任何处理,在调试时,它显示请求在过滤器处收到,但在该服务器生成400之后,请求永远不会转到
ServerEndpoint

Http过滤器:

@ServerEndpoint("/chat")
public class ChatServer {
    private Session session;

    @OnOpen
    public void connect(Session session){
        this.session=session;
    }

    @OnClose
    public void close(){
        this.session=null;
    }
    
    @OnMessage
    public void processMessage(String msg){
        System.out.println("msg = " + msg);;
        if(this.session!=null && this.session.isOpen()){
            try {
                this.session.getBasicRemote().sendText("this is from server");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
@Provider
@PreMatching
public class SecurityFilter implements ContainerRequestFilter, ContainerResponseFilter {
    @Override
    public void filter(ContainerRequestContext containerRequestContext) throws IOException {
    //some snipet to evaluate the JAX-RS and nothing to do with websocket requests
    //websocket request received here but it did not go toward EndpointServer
    }

    @Override
    public void filter(ContainerRequestContext containerRequestContext, ContainerResponseContext containerResponseContext) throws IOException {
        System.out.println("response status: "+ containerResponseContext.getStatus());
        //here I get Http status code 404 
    }
}
public class SocketSmokeTest {
    private WebSocketContainer container;
    private HelloEndpoint endpoint;

    @Test
    public void communicate() {
        try {
            Session connectToServer = container.connectToServer
            (this.endpoint, new URI("ws://127.0.0.1:8080/gateway/chat"));
            //I have tried other path like "ws://127.0.0.1:8080/rootsource/gateway/chat"
            //or "ws://127.0.0.1:8080/rootsource/chat"
            this.endpoint.sendMessage("hello from client");
            Thread.sleep(3000);
        } catch (URISyntaxException e) {
            e.printStackTrace();
        } catch (DeploymentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
GET /resources/websocket/chatserver/ HTTP/1.1
Connection: Upgrade
Host: 127.0.0.1:8080
Origin: http://127.0.0.1:8080
Sec-WebSocket-Key: K3mTSnyiVbi5/yeyIwFR2Q==
Sec-WebSocket-Version: 13
Upgrade: websocket

HTTP/1.1 404 Not Found
Connection: keep-alive
Content-Length: 0
Date: Sat, 05 Sep 2020 05:02:43 GMT
...
<dependencies>
    ...
</dependencies>
<build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-ear-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <defaultLibBundleDir>lib</defaultLibBundleDir>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                        </manifest>
                    </archive>
                    <modules>
                        <ejbModule>
                            <groupId>ir.co.isc</groupId>
                            <artifactId>BusinessLayer</artifactId>
                        </ejbModule>
                        <ejbModule>
                            <groupId>ir.co.isc</groupId>
                            <artifactId>DataAccessLayer</artifactId>
                        </ejbModule>
                        <ejbModule>
                            <groupId>ir.co.isc</groupId>
                            <artifactId>PresentationLayerGateway</artifactId>
                        </ejbModule>
                        <webModule>
                            <groupId>ir.co.isc</groupId>
                            <artifactId>PresentationLayerGUI</artifactId>
                            <contextRoot>/rootsource</contextRoot>
                        </webModule>
                    </modules>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-ejb-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
            </plugin>
        </plugins>
    </pluginManagement>
</build>
...
<?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">
    <parent>
        <artifactId>Azmoonyar</artifactId>
        <groupId>ir.co.isc</groupId>
        <version>1.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>PresentationLayerGateway</artifactId>
    <packaging>ejb</packaging>

    <name>PresentationLayerGateway</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>ir.co.isc</groupId>
            <artifactId>BusinessLayer</artifactId>
            <version>1.0</version>
            <type>ejb</type>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>8.0.1</version>
            <scope>provided</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/javax.ejb/ejb-api -->
        <dependency>
            <groupId>javax.ejb</groupId>
            <artifactId>ejb-api</artifactId>
            <version>3.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.websocket</groupId>
            <artifactId>javax.websocket-api</artifactId>
            <version>1.1</version>
        </dependency>
        <dependency>
            <groupId>javax.websocket</groupId>
            <artifactId>javax.websocket-client-api</artifactId>
            <version>1.1</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.tyrus</groupId>
            <artifactId>tyrus-client</artifactId>
            <version>1.17</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.glassfish.tyrus.bundles</groupId>
            <artifactId>tyrus-standalone-client</artifactId>
            <version>1.17</version>
        </dependency>
    </dependencies>

    <build>
        <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-ejb-plugin</artifactId>
                    <version>3.1.0</version>
                    <configuration>
                        <ejbVersion>3.0</ejbVersion>
                        <archive>
                            <manifest>
                                <addClasspath>true</addClasspath>
                            </manifest>
                        </archive>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.1</version   >
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>3.0.0-M3</version>
                    <configuration>
                        <skipTests>${skipTests}</skipTests>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>
我删除了过滤器,但错误仍然存在

客户端:

@ServerEndpoint("/chat")
public class ChatServer {
    private Session session;

    @OnOpen
    public void connect(Session session){
        this.session=session;
    }

    @OnClose
    public void close(){
        this.session=null;
    }
    
    @OnMessage
    public void processMessage(String msg){
        System.out.println("msg = " + msg);;
        if(this.session!=null && this.session.isOpen()){
            try {
                this.session.getBasicRemote().sendText("this is from server");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
@Provider
@PreMatching
public class SecurityFilter implements ContainerRequestFilter, ContainerResponseFilter {
    @Override
    public void filter(ContainerRequestContext containerRequestContext) throws IOException {
    //some snipet to evaluate the JAX-RS and nothing to do with websocket requests
    //websocket request received here but it did not go toward EndpointServer
    }

    @Override
    public void filter(ContainerRequestContext containerRequestContext, ContainerResponseContext containerResponseContext) throws IOException {
        System.out.println("response status: "+ containerResponseContext.getStatus());
        //here I get Http status code 404 
    }
}
public class SocketSmokeTest {
    private WebSocketContainer container;
    private HelloEndpoint endpoint;

    @Test
    public void communicate() {
        try {
            Session connectToServer = container.connectToServer
            (this.endpoint, new URI("ws://127.0.0.1:8080/gateway/chat"));
            //I have tried other path like "ws://127.0.0.1:8080/rootsource/gateway/chat"
            //or "ws://127.0.0.1:8080/rootsource/chat"
            this.endpoint.sendMessage("hello from client");
            Thread.sleep(3000);
        } catch (URISyntaxException e) {
            e.printStackTrace();
        } catch (DeploymentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
GET /resources/websocket/chatserver/ HTTP/1.1
Connection: Upgrade
Host: 127.0.0.1:8080
Origin: http://127.0.0.1:8080
Sec-WebSocket-Key: K3mTSnyiVbi5/yeyIwFR2Q==
Sec-WebSocket-Version: 13
Upgrade: websocket

HTTP/1.1 404 Not Found
Connection: keep-alive
Content-Length: 0
Date: Sat, 05 Sep 2020 05:02:43 GMT
...
<dependencies>
    ...
</dependencies>
<build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-ear-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <defaultLibBundleDir>lib</defaultLibBundleDir>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                        </manifest>
                    </archive>
                    <modules>
                        <ejbModule>
                            <groupId>ir.co.isc</groupId>
                            <artifactId>BusinessLayer</artifactId>
                        </ejbModule>
                        <ejbModule>
                            <groupId>ir.co.isc</groupId>
                            <artifactId>DataAccessLayer</artifactId>
                        </ejbModule>
                        <ejbModule>
                            <groupId>ir.co.isc</groupId>
                            <artifactId>PresentationLayerGateway</artifactId>
                        </ejbModule>
                        <webModule>
                            <groupId>ir.co.isc</groupId>
                            <artifactId>PresentationLayerGUI</artifactId>
                            <contextRoot>/rootsource</contextRoot>
                        </webModule>
                    </modules>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-ejb-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
            </plugin>
        </plugins>
    </pluginManagement>
</build>
...
<?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">
    <parent>
        <artifactId>Azmoonyar</artifactId>
        <groupId>ir.co.isc</groupId>
        <version>1.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>PresentationLayerGateway</artifactId>
    <packaging>ejb</packaging>

    <name>PresentationLayerGateway</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>ir.co.isc</groupId>
            <artifactId>BusinessLayer</artifactId>
            <version>1.0</version>
            <type>ejb</type>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>8.0.1</version>
            <scope>provided</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/javax.ejb/ejb-api -->
        <dependency>
            <groupId>javax.ejb</groupId>
            <artifactId>ejb-api</artifactId>
            <version>3.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.websocket</groupId>
            <artifactId>javax.websocket-api</artifactId>
            <version>1.1</version>
        </dependency>
        <dependency>
            <groupId>javax.websocket</groupId>
            <artifactId>javax.websocket-client-api</artifactId>
            <version>1.1</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.tyrus</groupId>
            <artifactId>tyrus-client</artifactId>
            <version>1.17</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.glassfish.tyrus.bundles</groupId>
            <artifactId>tyrus-standalone-client</artifactId>
            <version>1.17</version>
        </dependency>
    </dependencies>

    <build>
        <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-ejb-plugin</artifactId>
                    <version>3.1.0</version>
                    <configuration>
                        <ejbVersion>3.0</ejbVersion>
                        <archive>
                            <manifest>
                                <addClasspath>true</addClasspath>
                            </manifest>
                        </archive>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.1</version   >
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>3.0.0-M3</version>
                    <configuration>
                        <skipTests>${skipTests}</skipTests>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>
HelloEndPoint
是一个简单的类,如下所示:

public class HelloEndpoint extends Endpoint {
    private Session session;

    @Override
    public void onOpen(Session session, EndpointConfig endpointConfig) {
        this.session = session;

        this.session.addMessageHandler(new MessageHandler.Whole<String>() {
            @Override
            public void onMessage(String message) {
                System.out.println("!!!!!!  retrieved message: " + message);
            }
        });
    }

    public void sendMessage(String message){
        try {
            this.session.getBasicRemote().sendText(message);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
有关项目结构的更多信息: 该项目由多层方法中的一些模块组成,例如在EAR中聚合的数据(ejb模块)、业务(ejb模块)、网关(ejb模块)、GUI(war模块)。所有JAX-RS和Websocket都驻留在网关模块中

EAR pom文件:

@ServerEndpoint("/chat")
public class ChatServer {
    private Session session;

    @OnOpen
    public void connect(Session session){
        this.session=session;
    }

    @OnClose
    public void close(){
        this.session=null;
    }
    
    @OnMessage
    public void processMessage(String msg){
        System.out.println("msg = " + msg);;
        if(this.session!=null && this.session.isOpen()){
            try {
                this.session.getBasicRemote().sendText("this is from server");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
@Provider
@PreMatching
public class SecurityFilter implements ContainerRequestFilter, ContainerResponseFilter {
    @Override
    public void filter(ContainerRequestContext containerRequestContext) throws IOException {
    //some snipet to evaluate the JAX-RS and nothing to do with websocket requests
    //websocket request received here but it did not go toward EndpointServer
    }

    @Override
    public void filter(ContainerRequestContext containerRequestContext, ContainerResponseContext containerResponseContext) throws IOException {
        System.out.println("response status: "+ containerResponseContext.getStatus());
        //here I get Http status code 404 
    }
}
public class SocketSmokeTest {
    private WebSocketContainer container;
    private HelloEndpoint endpoint;

    @Test
    public void communicate() {
        try {
            Session connectToServer = container.connectToServer
            (this.endpoint, new URI("ws://127.0.0.1:8080/gateway/chat"));
            //I have tried other path like "ws://127.0.0.1:8080/rootsource/gateway/chat"
            //or "ws://127.0.0.1:8080/rootsource/chat"
            this.endpoint.sendMessage("hello from client");
            Thread.sleep(3000);
        } catch (URISyntaxException e) {
            e.printStackTrace();
        } catch (DeploymentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
GET /resources/websocket/chatserver/ HTTP/1.1
Connection: Upgrade
Host: 127.0.0.1:8080
Origin: http://127.0.0.1:8080
Sec-WebSocket-Key: K3mTSnyiVbi5/yeyIwFR2Q==
Sec-WebSocket-Version: 13
Upgrade: websocket

HTTP/1.1 404 Not Found
Connection: keep-alive
Content-Length: 0
Date: Sat, 05 Sep 2020 05:02:43 GMT
...
<dependencies>
    ...
</dependencies>
<build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-ear-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <defaultLibBundleDir>lib</defaultLibBundleDir>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                        </manifest>
                    </archive>
                    <modules>
                        <ejbModule>
                            <groupId>ir.co.isc</groupId>
                            <artifactId>BusinessLayer</artifactId>
                        </ejbModule>
                        <ejbModule>
                            <groupId>ir.co.isc</groupId>
                            <artifactId>DataAccessLayer</artifactId>
                        </ejbModule>
                        <ejbModule>
                            <groupId>ir.co.isc</groupId>
                            <artifactId>PresentationLayerGateway</artifactId>
                        </ejbModule>
                        <webModule>
                            <groupId>ir.co.isc</groupId>
                            <artifactId>PresentationLayerGUI</artifactId>
                            <contextRoot>/rootsource</contextRoot>
                        </webModule>
                    </modules>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-ejb-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
            </plugin>
        </plugins>
    </pluginManagement>
</build>
...
<?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">
    <parent>
        <artifactId>Azmoonyar</artifactId>
        <groupId>ir.co.isc</groupId>
        <version>1.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>PresentationLayerGateway</artifactId>
    <packaging>ejb</packaging>

    <name>PresentationLayerGateway</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>ir.co.isc</groupId>
            <artifactId>BusinessLayer</artifactId>
            <version>1.0</version>
            <type>ejb</type>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>8.0.1</version>
            <scope>provided</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/javax.ejb/ejb-api -->
        <dependency>
            <groupId>javax.ejb</groupId>
            <artifactId>ejb-api</artifactId>
            <version>3.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.websocket</groupId>
            <artifactId>javax.websocket-api</artifactId>
            <version>1.1</version>
        </dependency>
        <dependency>
            <groupId>javax.websocket</groupId>
            <artifactId>javax.websocket-client-api</artifactId>
            <version>1.1</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.tyrus</groupId>
            <artifactId>tyrus-client</artifactId>
            <version>1.17</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.glassfish.tyrus.bundles</groupId>
            <artifactId>tyrus-standalone-client</artifactId>
            <version>1.17</version>
        </dependency>
    </dependencies>

    <build>
        <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-ejb-plugin</artifactId>
                    <version>3.1.0</version>
                    <configuration>
                        <ejbVersion>3.0</ejbVersion>
                        <archive>
                            <manifest>
                                <addClasspath>true</addClasspath>
                            </manifest>
                        </archive>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.1</version   >
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>3.0.0-M3</version>
                    <configuration>
                        <skipTests>${skipTests}</skipTests>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>
。。。
...
org.apache.maven.plugins
maven耳朵插件
2.6
解放党
真的
ir.co.isc
业务层
ir.co.isc
数据访问层
ir.co.isc
演示Ayergateway
ir.co.isc
演示Ayergui
/根源
org.apache.maven.plugins
maven战争插件
org.apache.maven.plugins
maven ejb插件
org.apache.maven.plugins
maven编译器插件
...
网关EJB模块pom文件:

@ServerEndpoint("/chat")
public class ChatServer {
    private Session session;

    @OnOpen
    public void connect(Session session){
        this.session=session;
    }

    @OnClose
    public void close(){
        this.session=null;
    }
    
    @OnMessage
    public void processMessage(String msg){
        System.out.println("msg = " + msg);;
        if(this.session!=null && this.session.isOpen()){
            try {
                this.session.getBasicRemote().sendText("this is from server");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
@Provider
@PreMatching
public class SecurityFilter implements ContainerRequestFilter, ContainerResponseFilter {
    @Override
    public void filter(ContainerRequestContext containerRequestContext) throws IOException {
    //some snipet to evaluate the JAX-RS and nothing to do with websocket requests
    //websocket request received here but it did not go toward EndpointServer
    }

    @Override
    public void filter(ContainerRequestContext containerRequestContext, ContainerResponseContext containerResponseContext) throws IOException {
        System.out.println("response status: "+ containerResponseContext.getStatus());
        //here I get Http status code 404 
    }
}
public class SocketSmokeTest {
    private WebSocketContainer container;
    private HelloEndpoint endpoint;

    @Test
    public void communicate() {
        try {
            Session connectToServer = container.connectToServer
            (this.endpoint, new URI("ws://127.0.0.1:8080/gateway/chat"));
            //I have tried other path like "ws://127.0.0.1:8080/rootsource/gateway/chat"
            //or "ws://127.0.0.1:8080/rootsource/chat"
            this.endpoint.sendMessage("hello from client");
            Thread.sleep(3000);
        } catch (URISyntaxException e) {
            e.printStackTrace();
        } catch (DeploymentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
GET /resources/websocket/chatserver/ HTTP/1.1
Connection: Upgrade
Host: 127.0.0.1:8080
Origin: http://127.0.0.1:8080
Sec-WebSocket-Key: K3mTSnyiVbi5/yeyIwFR2Q==
Sec-WebSocket-Version: 13
Upgrade: websocket

HTTP/1.1 404 Not Found
Connection: keep-alive
Content-Length: 0
Date: Sat, 05 Sep 2020 05:02:43 GMT
...
<dependencies>
    ...
</dependencies>
<build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-ear-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <defaultLibBundleDir>lib</defaultLibBundleDir>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                        </manifest>
                    </archive>
                    <modules>
                        <ejbModule>
                            <groupId>ir.co.isc</groupId>
                            <artifactId>BusinessLayer</artifactId>
                        </ejbModule>
                        <ejbModule>
                            <groupId>ir.co.isc</groupId>
                            <artifactId>DataAccessLayer</artifactId>
                        </ejbModule>
                        <ejbModule>
                            <groupId>ir.co.isc</groupId>
                            <artifactId>PresentationLayerGateway</artifactId>
                        </ejbModule>
                        <webModule>
                            <groupId>ir.co.isc</groupId>
                            <artifactId>PresentationLayerGUI</artifactId>
                            <contextRoot>/rootsource</contextRoot>
                        </webModule>
                    </modules>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-ejb-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
            </plugin>
        </plugins>
    </pluginManagement>
</build>
...
<?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">
    <parent>
        <artifactId>Azmoonyar</artifactId>
        <groupId>ir.co.isc</groupId>
        <version>1.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>PresentationLayerGateway</artifactId>
    <packaging>ejb</packaging>

    <name>PresentationLayerGateway</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>ir.co.isc</groupId>
            <artifactId>BusinessLayer</artifactId>
            <version>1.0</version>
            <type>ejb</type>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>8.0.1</version>
            <scope>provided</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/javax.ejb/ejb-api -->
        <dependency>
            <groupId>javax.ejb</groupId>
            <artifactId>ejb-api</artifactId>
            <version>3.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.websocket</groupId>
            <artifactId>javax.websocket-api</artifactId>
            <version>1.1</version>
        </dependency>
        <dependency>
            <groupId>javax.websocket</groupId>
            <artifactId>javax.websocket-client-api</artifactId>
            <version>1.1</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.tyrus</groupId>
            <artifactId>tyrus-client</artifactId>
            <version>1.17</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.glassfish.tyrus.bundles</groupId>
            <artifactId>tyrus-standalone-client</artifactId>
            <version>1.17</version>
        </dependency>
    </dependencies>

    <build>
        <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-ejb-plugin</artifactId>
                    <version>3.1.0</version>
                    <configuration>
                        <ejbVersion>3.0</ejbVersion>
                        <archive>
                            <manifest>
                                <addClasspath>true</addClasspath>
                            </manifest>
                        </archive>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.1</version   >
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>3.0.0-M3</version>
                    <configuration>
                        <skipTests>${skipTests}</skipTests>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

阿兹穆尼亚
ir.co.isc
1
4.0.0
演示Ayergateway
ejb
演示Ayergateway
http://www.example.com
UTF-8
1.8
1.8
ir.co.isc
业务层
1
ejb
假如
朱尼特
朱尼特
${junit.version}
测试
爪哇
JavaEEAPI
8.0.1
假如
javax.ejb
EJBAPI
3
假如
javax.websocket
javax.websocket-api
1.1
javax.websocket
javax.websocket-client-api
1.1
org.glassfish.tyrus
泰勒斯客户
1.17
测试
org.glassfish.tyrus.bundles
tyrus独立客户端
1.17
org.apache.maven.plugins
maven ejb插件
3.1.0
3
真的
org.apache.maven.plugins
maven编译器插件
3.8.1
1.8
1.8
org.apache.maven.plugins
maven surefire插件
3.0.0-M3
${skipTests}

您的web应用程序是如何部署的?部署期间指定了什么上下文路径?不要在注释中发布代码。编辑问题并在那里显示格式良好的pom。亲爱的@Andreas,您可以看到更新的问题…
/resources/websocket/chatserver
/resources/websocket/chatserver/
是两个不同的端点。您的代码实现了第一个地址,但请求是针对第二个地址的,因此找不到它(404)。@Andreas我尝试了一个解决方案,在这里发布:,对于这两个地址,我都得到404,您的web应用如何部署?部署期间指定了什么上下文路径?不要在注释中发布代码。编辑问题并在那里显示格式良好的pom。亲爱的@Andreas,您可以看到更新的问题…
/resources/websocket/chatserver
/resources/websocket/chatserver/
是两个不同的端点。您的代码实现了第一个,但请求是针对第二个,因此找不到它(404)。@Andreas我尝试了一个解决方案,在这里发布:,对于这两个地址,我都得到404