Java Servlet(Tomcat 9)中的WebSocket无需类路径扫描

时间:2018-09-23 13:04:01

标签: java websocket java-websocket

我试图在不进行类路径扫描的情况下在Java Servlet(Tomcat 9)中设置WebSocket,但是我被这个错误所困扰:

 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 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类:

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

1 个答案:

答案 0 :(得分:0)

我的web.xml已配置为使得Tomcat不扫描类路径(用于servlet等),但是它仍在扫描WebSocket内容

我的解决方案?只需删除WebSocket extends ServletContextListener类。