如何解决Spring Boot、tomcat、rest api 404
我正在使用 Kotlin + Gradle 并尝试构建一个用于在 Tomcat 上部署的 war 文件。我的应用程序来自 https://start.spring.io 加上一个简单的控制器,并使用 ./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())
}
}
当我尝试访问它时,我得到
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 Boot 应用程序带有内置的 Servlet。在 IDE 中启动应用程序时,您可能已经在使用此功能。
这基本上意味着您可以在任何 Web 服务器上运行您的 .jar 文件,并且无需设置额外的 tomcat 实例即可运行。
但是,如果您想将 Spring Boot 应用程序构建为 war 文件并将其部署到外部 tomcat,则需要执行一些额外的步骤,如 this article 中所述。
,假设从你到目前为止发布的内容来看:返回的路径在你的实际控制器路由“/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)可能会透露更多有关该问题的信息。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。