Spring boot、tomcat、rest api 404

Spring boot、tomcat、rest api 404,spring,spring-boot,kotlin,tomcat,war,Spring,Spring Boot,Kotlin,Tomcat,War,我正在使用Kotlin+Gradle,并试图构建一个war文件来部署在Tomcat上。我的应用程序来自plus一个简单的控制器,并使用/gradlew bootWar @SpringBootApplication class ServletInitializer : SpringBootServletInitializer() { override fun configure(application: SpringApplicationBuilder): SpringApplicati

我正在使用Kotlin+Gradle,并试图构建一个war文件来部署在Tomcat上。我的应用程序来自plus一个简单的控制器,并使用
/gradlew bootWar

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

我被卡住了。我做错了什么?如果我将一个html文件添加到
src/main/webapp/index.html
中,由于某种原因,只会显示rest api无法访问。

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

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


但是,如果您想将Spring Boot应用程序构建为war文件并将其部署到外部tomcat,则需要按照中的说明执行一些额外的步骤。

假设您目前发布的内容:返回的路径显示实际控制器路由之前的另一个路由“/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)可能会透露更多关于这个问题的信息。

有点离题,但您决定不在Spring引导中使用嵌入式servlet有什么特别的原因吗?您可能需要配置Tomcat的
web.xml
(servlet/servlet映射)嘿@TimTong谢谢您的回复我不确定不使用嵌入式servlet是什么意思您能详细说明一下吗?感谢您有两种部署应用程序的方式。选项1:您可以运行一个接受WAR的外部Tomcat(这是spring引导之前更传统的方法)。选项2:您可以运行一个JAR,其中包含嵌入的servlet(Tomcat)。这是Spring boot现在更典型的方法。啊,是的,我需要在远程服务器上使用现有的tomcat设置,所以我想在本地模拟它。我以前看过这篇文章,并遵循其中的步骤,但它仍然给我带来相同的问题:(
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.