2
votes

I made two classes:

  1. Main class with embedded Tomcat(8.5.20)
  2. ServerEndpoint of Websocket

I run the main class on IntelliJ IDEA and run this JavaScript: new WebSocket('ws://localhost:8080/ws') in the console of Google Chrome.

I expected the response code is 200, but actually it is 404.

How can I fix this?

Main class:

package webapp;


import org.apache.catalina.LifecycleException;
import org.apache.catalina.startup.Tomcat;

import javax.servlet.ServletException;
import java.io.File;

public class Main {
    private static final String STATIC_DIR = "src/main/static/";

    public static void main(String[] args) throws ServletException, LifecycleException {
        Tomcat tomcat = new Tomcat();
        tomcat.setPort(8080);

        File staticDir = new File(STATIC_DIR);
        tomcat.addWebapp("/", staticDir.getAbsolutePath());

        tomcat.start();
        tomcat.getServer().await();
    }
}

ServerEndpoint:

package webapp.websocket;

import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;

@ServerEndpoint("/ws")
public class SampleWebSocket {

    @OnOpen
    public void onOpen(Session session) {
        System.out.println("open");
    }


    @OnClose
    public void onClose(Session session) {
        System.out.println("close");
    }

    @OnMessage
    public String onMessage(String text) {
        System.out.println("message:" + text);
        return "Server:" + text;
    }
}

Thank you.

3

3 Answers

4
votes

I found a solution. This question is dupricated.

I read Got 404 error on tomcat 7.0.47 websocket and editted my pom.xml.

Following is the whole of my pom.xml.

<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>com.example</groupId>
    <artifactId>websocket-sample</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <name>websocket-sample</name>

    <properties>
        <java.version>1.8</java.version>
        <maven.compiler.target>${java.version}</maven.compiler.target>
        <maven.compiler.source>${java.version}</maven.compiler.source>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-core</artifactId>
            <version>8.5.20</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-websocket</artifactId>
            <version>8.5.20</version>
        </dependency>
        <dependency>
            <groupId>javax.websocket</groupId>
            <artifactId>javax.websocket-api</artifactId>
            <version>1.1</version>
        </dependency>
    </dependencies>
</project>
3
votes

Just add the following dependency to pom.xml

<dependency>
    <groupId>org.apache.tomcat</groupId>
    <artifactId>tomcat-websocket</artifactId>
    <version>8.5.20</version>
</dependency>
0
votes

What solved the issue for me was switching from the javax.websocket-api dependency to the Tomcat-specific tomcat-embed-websocket dependency instead and switching all my imports (ServerEndpoint, onMessage, etc.) to the Tomcat-specific jakarta.websocket.* packages instead of the javax.websocket.x packages.

Here is the endpoint code that worked for me -- notice that there are no javax.websocket imports and instead I have jakarta.websocket imports:

import java.io.IOException;

import jakarta.websocket.EncodeException;
import jakarta.websocket.OnMessage;
import jakarta.websocket.Session;
import jakarta.websocket.server.ServerEndpoint;

@ServerEndpoint("/echo")
public class EchoEndpoint {
    // Implementation.
    @OnMessage
    public void onMessage(
        Session session, 
        String message) 
        throws 
            IOException,
            EncodeException {
        // Send the same message back.
        session.getBasicRemote().sendText(message);
    }
}

And my pom.xml file dependencies:

<!-- https://mvnrepository.com/artifact/org.apache.tomcat.embed/tomcat-embed-core -->
<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-core</artifactId>
    <version>10.1.0-M8</version>
</dependency>           
<!-- https://mvnrepository.com/artifact/org.apache.tomcat.embed/tomcat-embed-websocket -->
<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-websocket</artifactId>
    <version>10.1.0-M8</version>
</dependency>   

As far as I can tell, the pom.xml should not contain the javax.websocket dependency at all, to ensure there are no collisions/clashes between that one and the Tomcat-specific tomcat-embed-websocket dependency.