0

First of all I open start.spring.io and choose project options:

  • Maven, Java, and the latest stable Spring Boot (2.4.5)
  • Group and artifact leave as default, packaging Jar, Java 11
  • In the Dependencies box select Spring Web

Then click Generate Project and download zip file.

I change pom.xml to

<?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.4.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <packaging>war</packaging>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>11</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>${project.artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

Also change DemoApplication.java from /src/main/java to

package com.example.demo;

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;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class DemoApplication extends SpringBootServletInitializer {

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

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @RequestMapping(value = "/")
    public String hello() {
        return "Hello from Tomcat";
    }
}

In the command line start ./mvnv package and see BUILD SUCCESS. After all execute catalina start and go to localhost:8080 in the browser. Select link Manager app and deploy demo.war from /target.

After all I try to check application with localhost:8080/demo and see in browser 404 page. Can someone explain to me why 404 page appear?

PS Operation system MacOS

Evgeniy
  • 3,219
  • 5
  • 25
  • 40
  • It seems to be client side error ... the page you are requesting does not exist .. most probably issue in your url http://localhost:8080/demo .. Try different urls – Sagii Apr 23 '21 at 13:19
  • @Sagii As I understand when deploy applicationName it can access from localhost:8080/applicationName/. Root URL is handled by @RequestMapping(value = "/") and must show `Hello from Tomcat`. If I start application with `./mvnw spring-boot:run` it shows such message on `localhost:8080`. – Evgeniy Apr 23 '21 at 13:24
  • 2
    Which Tomcat version are you using? My guess is Tomcat 10 (which is not compatible with Spring Boot 2.x, it will be with Spring Boot 3.x). – Piotr P. Karwasz Apr 23 '21 at 14:00
  • @PiotrP.Karwasz Thank you very much! Can you write this as an answer? – Evgeniy Apr 23 '21 at 18:11
  • 1
    I marked the question as duplicate of [Tomcat 10.0.4 doesn't load servlets (@WebServlet classes) with 404 error](https://stackoverflow.com/q/66806582/11748454) to limit the amount of question which revolve around the same problem. – Piotr P. Karwasz Apr 23 '21 at 18:32

0 Answers0