将spring boot websocket示例应用程序部署到tomcat7

时间:2015-03-09 11:00:39

标签: spring maven tomcat7 spring-boot

以下教程帮助我快速启动弹簧启动应用程序并快速运行:https://spring.io/guides/gs/messaging-stomp-websocket/

但现在我不得不尝试生成war文件并将其部署在我的tomcat7应用服务器上。

我按照[instructions] [1]创建了一个可部署的war文件,但这只是不起作用。我没有在日志中看到任何错误,但是当我浏览http://localhost:8080时,我看不到我的样本应用程序。

以下是我生成war文件的步骤:

1)修改pom.xml,方法是将包装更改为war并标记spring-boot-starter-websocket

<packaging>war</packaging>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-websocket</artifactId>
        <scope>provided</scope>            
    </dependency>            
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-messaging</artifactId>
    </dependency>
</dependencies>

2)修改Application.java以覆盖SpringBootServletInitializer配置方法。

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

3)然后我跑

  

mvn clean package

并部署生成的war文件,但是当我浏览到http://localhost:8080/时,我只是继续获取​​默认的Tomcat欢迎页面。

我还尝试通过添加以下application.properties来改变应用程序contextPath:

server.contextPath=/websocketclient
server.port=8082

但这并没有解决问题。

3 个答案:

答案 0 :(得分:1)

我正在使用它,我正在使用Tomcat 8。

在这里查看我的代码: https://github.com/pauldeng/gs-messaging-stomp-websocket

相应地阅读并编辑pom文件并运行 mvn package 以创建指定的包。

答案 1 :(得分:0)

部署的应用程序war需要调用ROOT.war,否则你必须配置server.xml。

其次,您应该打开websockets的NIO连接器,请参阅server.xml和http://tomcat.apache.org/tomcat-7.0-doc/web-socket-howto.html

答案 2 :(得分:0)

我在我的pom.xml中犯了一个错误,所提供的范围不应该添加到spring-boot-starter-websocket artefact中。但是你应该使用提供的范围添加spring-boot-starter-tomcat artefact。

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-websocket</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-messaging</artifactId>
    </dependency>
</dependencies>

我还必须修改我的javascript以联系websocket,现在在/gs-messaging-stomp-websocket-0.1.0下

var socket = new SockJS('/gs-messaging-stomp-websocket-0.1.0/hello');