skip to Main Content

I’m trying to deploy a simple spring boot project on tomcat server.

Locally via Intellij IDEA everything is working.

This is my RestController:

@RestController
@RequestMapping("/spring-mvc")
public class HomeController {

    @GetMapping("/index")
    public String homePage() {
        return "<p style='color: green;'>Hello, World</p>";
    }
}

This is my Main Class

@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Main extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Main.class);
    }
    public static void main(String[] args) {
        SpringApplication.run(Main.class, args);
    }
}

This is the 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.0.2</version>
        <relativePath/> 
    </parent>
    <groupId>com.visionthing</groupId>
    <artifactId>spring-mvc</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-mvc</name>
    <packaging>war</packaging>
    <properties>
        <java.version>19</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</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>6.0.4</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>3.0.2</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>
    </dependencies>

    <build>
        <finalName>spring-mvc</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

I exported the war via maven cli and moved it to the /webapps folder of my tomcat installation.

tomcat is running openjdk 19 as well as my local project.

When opening tomcat webui manager the folder shows up, clicking on it redirects me to http://localhost:8081/spring-mvc/ this site displays the 404 error page of tomcat.
as well as
http://localhost:8081/spring-mvc/index
http://localhost:8081/spring-mvc/spring-mvc/index

I was expecting to see Hello, World written in green on http://localhost:8081/spring-mvc/index

did i forget something?

thanks in regard.

i’m running linux pop-os based on ubuntu 22.04 LTS

2

Answers


  1. you need to add Main.class to application.sources(HomeController.class) instead of HomeController.class. Like this:
    application.sources(Main.class).

    Login or Signup to reply.
  2. mini spring boot war for spring boot 2.7.8

    Project Directory

    ├── pom.xml
    └── src
        └── main
            ├── java
            │   └── com
            │       └── visionthing
            │           └── springmvc
            │               ├── HomeController.java
            │               └── SpringMvcApplication.java
            ├── resources
            │   └── application.properties
            └── webapp
                └── WEB-INF
                    └── jsp
    

    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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.7.8</version>
            <!--<version>3.0.2</version>-->
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.visionthing</groupId>
        <artifactId>spring-mvc</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>war</packaging>
        <name>spring-mvc</name>
        <description>Demo project for Spring Boot</description>
        <properties>
            <java.version>19</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-tomcat</artifactId>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
        <build>
            <finalName>spring-mvc</finalName>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    
    </project>
    

    SpringMvcApplication.java

    package com.visionthing.springmvc;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.builder.SpringApplicationBuilder;
    import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
    
    @SpringBootApplication
    public class SpringMvcApplication extends SpringBootServletInitializer {
        @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
            return application.sources(SpringMvcApplication.class);
        }
        
        public static void main(String[] args) {
    
            SpringApplication.run(SpringMvcApplication.class, args);
        }
    
    }
    

    HomeController.java

    package com.visionthing.springmvc;
    
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    @RequestMapping("/spring-mvc")
    public class HomeController {
    
        @GetMapping("/index")
        public String homePage() {
            return "<p style='color: green;'>Hello, World</p>";
        }
    }
    

    application.properties (optional)

    spring.mvc.view.prefix=/WEB-INF/jsp/
    spring.mvc.view.suffix=.jsp
    

    package

    mvn clean package
    

    put spring-mvc.war to tomcat/webapps

    start tomcat

    test

    command line test: curl http://localhost:8081/spring-mvc/spring-mvc/index

    browser test open : http://localhost:8081/spring-mvc/spring-mvc/index

    conclusion

    Why I am change spring boot to 2.7.8 ?

    Because you can test the same project and the same code , just change pom.xml

    Spring boot version to 3.0.2 , and re-package war , you will get 404 again.

    Spring Boot 3.x require JDK 17 , but your pom.xml set source and target to version 1.8.

    Spring Boot 3.x require Tomcat 10.x , but you use Tomcat 8.x.

    So I can only provide a minimum example project that can be executed, for your limitation Tomcat 8.x, source and target is set to 1.8.

    In addition, I removed irrelevant dependencies in pom.xml.


    UPDATE For Spring Boot 3.0.2

    1. Change pom.xml,
    • spring boot version to 3.0.2
    • remove compiler configuration : source , target 1.8
    1. Change Tomcat
    • Use Tomcat 10.x

    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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <!--<version>2.7.8</version>-->
            <version>3.0.2</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.visionthing</groupId>
        <artifactId>spring-mvc</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>war</packaging>
        <name>spring-mvc</name>
        <description>Demo project for Spring Boot</description>
        <properties>
            <java.version>19</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-tomcat</artifactId>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
        <build>
            <finalName>spring-mvc</finalName>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>
    

    Tomcat 10.x

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search