1

I'm using spring boot with a simple hello world controller. I would like to deploy it to external tomcat server. I've noticed that web.xml file is missing. I expected to see this file inside web-inf directory. When running spring boot internally (with the internal tomcat of spring boot, it works. But I would like to deploy my app to external tomcat server.

After deploying the war file to external tomcat server, I can see the hello world in the available services list, but I can't use the hello world service - I get 404 error message instead.

I'm using maven, Jdk 1.8, IntelliJ

This is my pom.xml:

<?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>
<packaging>war</packaging>

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

<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>
    <dependency>
        <groupId>org.apache.maven</groupId>
        <artifactId>maven-plugin-api</artifactId>
        <version>3.3.9</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
        <version>1.9.2.RELEASE</version>
    </dependency>

</dependencies>

<properties>
    <java.version>1.8</java.version>
</properties>


<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.6</version>
            <configuration>
                <attachClasses>true</attachClasses>
            </configuration>
        </plugin>
    </plugins>
</build>

</project>

This is my Application class:

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

@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);
        }
    }
}

What is the right way to deploy the hello world app to external tomcat service?

Stafford Williams
  • 9,696
  • 8
  • 50
  • 101

2 Answers2

0

When deploying on tomcat, there should be a context path added by the container and that have to be included as first element path in your URL. Check tomcat startup logs to get the context path and retry calling the service as indicated.

Guy Bouallet
  • 2,099
  • 11
  • 16
0

If Spring boot app on external Tomcat is deployed successfully and no error are thrown, it is reachable not through localhost:8080/ but localhost:8080/your-deployed-war-file-name.

You can add finalName to your pom.xml to avoid adding versions to built war filename - Maven : How to avoid version appended to a war file in Maven?:

<build>
    <finalName>${project.artifactId}</finalName>
</build>
Justinas Jakavonis
  • 8,220
  • 10
  • 69
  • 114