springboot项目中的websocket和stomp没有'javax.websocket.server.ServerContainer'ServletContext属性错误

时间:2019-12-06 17:18:36

标签: spring spring-boot websocket stomp

这个问题来自 relevant details。在springboot项目中使用websocket和stomp时,出现No 'javax.websocket.server.ServerContainer' ServletContext attribute错误。

    // org.springframework.web.socket.server.standard.AbstractStandardUpgradeStrategy.java
    protected ServerContainer getContainer(HttpServletRequest request) {
        ServletContext servletContext = request.getServletContext();
        String attrName = "javax.websocket.server.ServerContainer";
        ServerContainer container = (ServerContainer) servletContext.getAttribute(attrName);

        // container is null
        Assert.notNull(container, "No 'javax.websocket.server.ServerContainer' ServletContext attribute. " +
                "Are you running in a Servlet container that supports JSR-356?");
        return container;
    }

我尝试了以下解决方案,但无济于事。

// layout of project
projectname
       --src/main/webapp/WEB-INF/web.xml // created manually
       --module1/src/main/java/Application.java // Class with annotiation @SpringBootApplication
       --module2/src/main/java/...

web.xml

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="
        http://java.sun.com/xml/ns/javaee
        http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">

    <absolute-ordering>
        <name>spring_web</name>
    </absolute-ordering>

</web-app>

1 个答案:

答案 0 :(得分:0)

this discussion我了解到,此问题的fix已包含在Tomcat 9.0.31、8.5.51和7.0.100版本中。