Tomcat 部署 Spring 引导出现 404

Tomcat Deployment of Spring Boot is giving 404

我正在尝试在 Tomcat 8 上部署 Spring Boot Rest Controller。当我直接从 Eclipse 运行 它时,它工作正常。但是当部署到 Tomcat 8 时,出现 404 错误。

我已按照这些说明 (https://www.baeldung.com/spring-boot-war-tomcat-deploy) 进行了搜索,但似乎没有任何效果。

这是我的控制器:

import java.util.Collection;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TomcatController {

    @GetMapping("/hello")
    public Collection<String> sayHello() {
        return IntStream.range(0, 10)
          .mapToObj(i -> "Hello number " + i)
          .collect(Collectors.toList());
    }
    
}

这是我的申请:

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 SsoApplication extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(SsoApplication.class, args);
    }
    
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(SsoApplication.class);
    }
}

这是我的 pom.xml:

<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>
  <groupId>com.test</groupId>
  <artifactId>sso</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>SSOServices</name>
  <description>SSO Services</description>
  <packaging>war</packaging>
  
    <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId> 
      <version>2.4.0</version> 
      <relativePath/> 
    </parent> 
    <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>
    </dependencies>
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
        <finalName>${artifactId}</finalName>
    </build>
    
</project>

如有任何帮助,我们将不胜感激。 总而言之,当我使用“localhost:8080/hello”通过 eclipse 进行测试时,它有效。 当我部署到 tomcat 8 并使用“localhost:8080/sso/hello”进行测试时,我得到一个 404。

来自 M. Deinum(上)

要使 Spring Boot 2.4 正常工作,您需要使用 Tomcat 8.5 作为最低(不只是 8)甚至更好 tomcat 9.