0
votes

I've installed tomcat on archlinux, I tried both tomcat7 and tomcat8. According to several sources, including the official documentation, deploying a WAR file is as easy as dropping it in the webapps folder (which in my case is /var/lib/tomcat7/webapps). The WAR file gets exploded. What I can't figure out though is how to access my web application. On localhost:8080 there is a tomcat webpage. I also tried localhost:8080/name-of-the-war-file, but that only let to a HTTP Status 404.

The application I've used for testing is the first guide on spring boot: http://spring.io/guides/gs/spring-boot/

I've modified the maven build file pom.xml to produce a WAR file when running 'mvn package':

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.springframework</groupId>
    <artifactId>gs-spring-boot</artifactId>
    <version>0.1.0</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.2.2.RELEASE</version>
    </parent>

    <packaging>war</packaging>

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

    <properties>
        <java.version>1.7</java.version>
    </properties>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
1
hm.. I was hoping this was an easy one...user1785730
Huh, a downvote? Did you confuse the buttons?user1785730

1 Answers

0
votes

Apart from modifying the pom.xml to produce a WAR file, it is also neccessary to configure tomcat for the web application. This was formerly done via a web.xml file. Nowadays this can be archieved from within the application itself. SpringBootServletInitializer and WebApplicationInitializer are the cues to look for. The easiest way I found to get I all running was to modify Application.java in the following way:

package hello;

import java.util.Arrays;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

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

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

        System.out.println("Let's inspect the beans provided by Spring Boot:");

        String[] beanNames = ctx.getBeanDefinitionNames();
        Arrays.sort(beanNames);
        for (String beanName : beanNames) {
            System.out.println(beanName);
        }
    }
}

Now I can see "Greetings from Spring Boot!" on http://localhost:8080/gs-spring-boot-0.1.0/ (with gs-spring-boot-0.1.0.war being the name of the deployed WAR file).