在没有 web.xml 的情况下将 Spring 应用程序部署到 Tomcat

Deploy Spring App to Tomcat without web.xml

我已经成功测试了带有嵌入式 TOMCAT 的 war 文件,它工作正常。现在我想将它部署到 TOMCAT,但我不断收到 404 错误

我在浏览器上导航到的URL是 http://localhost:9999/springrest/demo/users/all

我的TOMCAT使用端口9999

我的JRE_HOME版本是1.8.0_261

我的pom.xml1.8

TOMCAT 经理显示该应用程序为 运行

我的控制器代码如下

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

}

我的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>

你是否通过实现SpringBootServletInitializer接口初始化了Tomcat需要的Servlet上下文?

@SpringBootApplication
public class SpringBootTomcatApplication extends SpringBootServletInitializer {
}

详情请参考这个link:Deploy a Spring Boot WAR into a Tomcat Server

将我的 Apache Tomcat 版本 更改为 Apache Tomcat 8.5.58 并且效果很好. Springboot 横幅甚至在 运行ning catalina.bat 运行

上的 cmd 上显示