Java Servlet (Tomcat 9) 中的 WebSocket 没有类路径扫描

WebSocket in Java Servlet (Tomcat 9) without classpath scanning

我试图在 Java Servlet (Tomcat 9) 中设置 WebSocket 而不进行 class 路径扫描,但我遇到了这个错误:

 javax.websocket.DeploymentException: Multiple Endpoints may not be deployed to the same path [/test/{accountId}] : existing endpoint was [class probotdev.websocket.Test] and new endpoint is [class probotdev.websocket.Test]
    at org.apache.tomcat.websocket.server.WsServerContainer.addEndpoint(WsServerContainer.java:169)
    at org.apache.tomcat.websocket.server.WsServerContainer.addEndpoint(WsServerContainer.java:232)

这是我的 web.xml:

<web-app version="3.0" metadata-complete="true" ...>
    <absolute-ordering/>
    <listener>
        <listener-class>probotdev.websocket.WebSocket</listener-class>
    </listener>
    ...
</web-app>

WebSocket class:

class WebSocket extends ServletContextListener {
    override def contextInitialized(sce: ServletContextEvent): Unit = {
        val container = sce.getServletContext.getAttribute("javax.websocket.server.ServerContainer").asInstanceOf[ServerContainer]

        container.addEndpoint(classOf[Test])
    }
}

最后 Test class:

@ServerEndpoint("/test")
class Test {
    @OnOpen
    def onOpen(session: Session): Unit = {
        println("Bla!")
    }
    ...
}

我的 web.xml 配置为 Tomcat 不扫描 class 路径(对于 servlet 等),但 它仍在扫描 WebSocket 内容.

我的解决方案?只需删除 WebSocket extends ServletContextListener class.