0
votes

Following tutorial helped me to get a spring boot application up and running real fast: https://spring.io/guides/gs/messaging-stomp-websocket/

But now I'm stuck trying to generate a war file and deploying it on my tomcat7 application server.

I followed the [instructions][1] to create a deployable war file, but this just isn't working. I don't see any errors in the logs, but I don't see my nice sample application either when I browse to http://localhost:8080.

Here are the steps I took to generate a war file:

1) Modify pom.xml by changing the packaging to war and marking the spring-boot-starter-websocket as provided

<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) Modify Application.java to override the SpringBootServletInitializer configure method.

@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) Then I run

mvn clean package

and deploy the generated war-file, but I just keep getting the default Tomcat welcome page, when I browse to http://localhost:8080/.

I also tried changing the application contextPath by adding the following application.properties:

server.contextPath=/websocketclient
server.port=8082

But this doesn't solve the problem.

3
Which version of Tomcat 7? You need 7.0.52+ for Web Socket - Neil McGuigan

3 Answers

1
votes

I get it working, I am using Tomcat 8.

Take a look at my code here: https://github.com/pauldeng/gs-messaging-stomp-websocket

Read and edit the pom file accordingly and run mvn package to create the package specified.

0
votes

I made a mistake in my pom.xml, the provided scope shouldn't be added to the spring-boot-starter-websocket artefact. But you should add the spring-boot-starter-tomcat artefact with the provided scope.

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

I also had to modify my javascript to contact the websocket, which is now under /gs-messaging-stomp-websocket-0.1.0

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

The deployed application war needs to be called ROOT.war otherwise you will have to configure server.xml.

Second you should turn on the NIO connector for websockets see server.xml and http://tomcat.apache.org/tomcat-7.0-doc/web-socket-howto.html