Search code examples
springtomcatspring-boottomcat8spring-web

Can not access deployed WAR file on Tomcat


I have a Spring boot rest service project which works on my local machine. When I run the application as "Spring Boot App"

I can access the rest service by going to

http://127.0.0.1:8080/persons/all

and it returns JSON as it's supposed to.

I changed the pom.xml packaging to war, I then created a war by going to Run as -> Maven build in Spring tools suit. It creates a war file. When I upload the war file on http://myserverip:8080/manager/ I get no errors and it shows up under Applications

The war file name is "myapp-0.0.1-SNAPSHOT.war" after uploading it, I tried to access it by going to

http://myserverip:8080/myapp-0.0.1-SNAPSHOT/persons/all

But I get a 404 error

What am I doing wrong here?

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>com.mycompany</groupId>
    <artifactId>myapp</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <name>myapp</name>
    <description>myappapi</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.2.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

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

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


    <packaging>war</packaging>
</project>

When I build the application, I get the following logs

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building myapp 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ myapp ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ myapp ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ myapp ---
[INFO] Not copying test resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ myapp ---
[INFO] Not compiling test sources
[INFO] 
[INFO] --- maven-surefire-plugin:2.18.1:test (default-test) @ myapp ---
[INFO] Tests are skipped.
[INFO] 
[INFO] --- maven-war-plugin:2.6:war (default-war) @ myapp ---
[INFO] Packaging webapp
[INFO] Assembling webapp [myapp] in [C:\Users\me\Documents\workspace-sts-3.8.2.RELEASE\myapp\target\myapp-0.0.1-SNAPSHOT]
[INFO] Processing war project
[INFO] Webapp assembled in [586 msecs]
[INFO] Building war: C:\Users\me\Documents\workspace-sts-3.8.2.RELEASE\myapp\target\myapp-0.0.1-SNAPSHOT.war
[INFO] 
[INFO] --- spring-boot-maven-plugin:1.4.2.RELEASE:repackage (default) @ myapp ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.300 s
[INFO] Finished at: 2016-11-21T10:12:03-05:00
[INFO] Final Memory: 20M/243M
[INFO] ------------------------------------------------------------------------
[WARNING] The requested profile "pom.xml" could not be activated because it does not exist.

my MyappApplication.java

package com.mycompany;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableAutoConfiguration
@ComponentScan
public class MyappApplication {
    public static void main(String[] args) {
        SpringApplication.run(MyappApplication.class, args);
    }
}

Solution

  • Refer: Packaging executable jar and war files

    As mentioned in comments, you can deploy spring boot application traditionally by deploying war to a standalone tomcat instance. You need to make some changes to your MyappApplication.java class which has the main() method.

    • Make your MyappApplication to extend SpringBootServletInitializer and override its configure() method,

      @SpringBootApplication
      public class MyappApplication extends SpringBootServletInitializer {
      
          @Override
          protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
              return application.sources(Application.class);
          }
      
          public static void main(String[] args) throws Exception {
              SpringApplication.run(MyappApplication.class, args);
          }
      
      }
      
    • The next step is to have packaging as war instead of the default jar which you already have.

    • Finally in your pom.xml change the embedded tomcat container's scope to provided, so that it doesn't conflict when you deploy to a separate instance.