Spring boot、tomcat、rest api 404

问题描述 投票:0回答:3

我正在使用 Kotlin + Gradle 并尝试构建一个 war 文件以部署在 Tomcat 上。我的应用程序来自 https://start.spring.io 加上一个简单的控制器并使用

./gradlew bootWar

构建 war 文件
@SpringBootApplication
class ServletInitializer : SpringBootServletInitializer() {

    override fun configure(application: SpringApplicationBuilder): SpringApplicationBuilder {
        return application.sources(DemoApplication::class.java)
    }

}

@RestController
class TomcatController {
    @GetMapping("/hello")
    fun sayHello(): Collection<String> {
        return IntStream.range(0, 10)
            .mapToObj { i: Int -> "Hello number $i" }
            .collect(Collectors.toList())
    }
}

当我尝试访问它时,我得到了

Type Status Report

Message The requested resource [/demo-0.0.1-SNAPSHOT/hello] is not available

Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.

我超级卡住了。我做错了什么?如果我将 html 文件添加到

src/main/webapp/index.html
,由于某种原因,它会显示只有其余 api 无法访问。

spring spring-boot kotlin tomcat war
3个回答
0
投票

Spring Boot 应用程序带有内置的 Servlet。在 IDE 中启动应用程序时,您可能已经在使用此功能。

这基本上意味着您可以在任何 Web 服务器上运行您的 .jar 文件,并且无需设置额外的 tomcat 实例即可运行。

但是,如果您想将 Spring Boot 应用程序构建为 war 文件并将其部署到外部 tomcat,则需要遵循本文中所述的一些额外步骤。


0
投票

假设您到目前为止发布的内容:返回的路径在您的实际控制器路由“/demo-0.0.1-SNAPSHOT/hello”之前显示了另一条路由,这“/demo-0.0.1-SNAPSHOT”是您的路径应用程序运行于?如果没有,它应该包含在您的控制器中(假设您没有在其他地方设置它,例如在您的 application.properties 中)。 例如http://localhost:8080/ 将是基本路径,http://localhost:8080/demo-0.0.1-SNAPSHOT/hello 或 http://localhost:8080/hello 将指向您的控制器。此外,您的启动日志(针对 Tomcat 和 Spring)可能会泄露有关该问题的更多信息。


0
投票

使用 Spring Boot 3.3.5 和 Java21 运行 Tomcat9 时,我收到了同样的错误消息。但是,当我升级到 Tomcat10 时,错误消失了。唯一的问题是,在 Ubuntu 上,您还需要至少升级到 24.04 才能从 apt 包管理获取 tomcat10。当然,您也可以进行普通安装。

© www.soinside.com 2019 - 2024. All rights reserved.