简体   繁体   中英

Deploy Spring App to Tomcat without web.xml

I have successfully tested my war file with embedded TOMCAT and it works fine. Now I want to deploy it to TOMCAT but I keep getting a 404 error .

The URL I navigate to on the browser is http://localhost:9999/springrest/demo/users/all

My TOMCAT uses the port 9999

My JRE_HOME version is 1.8.0_261

My pom.xml has <java.version>1.8</java.version>

TOMCAT manager shows the app as running 在此处输入图片说明

My Controller code is as follows

  @RestController
  @RequestMapping(path="/demo") 
  public class UserController {

  @Autowired 
  private UserRepository userRepository;

  @GetMapping(path="/users/all")
  public @ResponseBody Iterable<User> getAllUsers() {
  // This returns a JSON or XML with the users
  return userRepository.findAll();
  }

}

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 
 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.0.0.RELEASE</version>
    <relativePath/> 
</parent>
<groupId>com.eptc</groupId>
<artifactId>springrest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springrest</name>
<description>Rest API for mobile app</description>
<packaging>war</packaging>

<properties>
    <java.version>1.8</java.version>    
<start-class>com.eptc.springrest.SpringrestApplication</start-class>
</properties>

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

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>5.6.2</version>
        <scope>test</scope>
        <type>jar</type>
    </dependency> 
 <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>
</dependencies>

<build>
<finalName>${artifactId}</finalName>
    <plugins>
    <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
    </plugins>
</build>

Have you initialized the Servlet context required by Tomcat by implementing the SpringBootServletInitializer interface?

@SpringBootApplication
public class SpringBootTomcatApplication extends SpringBootServletInitializer {
}

Please refer this link for the details : Deploy a Spring Boot WAR into a Tomcat Server

Changed my Apache Tomcat version to Apache Tomcat 8.5.58 and it worked like a charm. The Springboot banner even shows on cmd on running catalina.bat run

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM