简体   繁体   English

Spring 启动,tomcat,rest Z8A5DA52ED126447D359E704C05721A8AAZ

[英]Spring boot, tomcat, rest api 404

I am using Kotlin + Gradle and trying to build a war file to deploy on Tomcat.我正在使用 Kotlin + Gradle 并尝试构建一个战争文件以部署在 Tomcat 上。 My application is from the https://start.spring.io plus a simple controller and build the war file using ./gradlew bootWar My application is from the https://start.spring.io plus a simple controller and build the war file using ./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())
    }
}

when I try to access it I get当我尝试访问它时,我得到

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.

I am super stuck.我超级卡住了。 What am I doing wrong?我究竟做错了什么? If I add a html file to the src/main/webapp/index.html it shows up for some reason only the rest api can't be reached. If I add a html file to the src/main/webapp/index.html it shows up for some reason only the rest api can't be reached.

Spring Boot applications come with a built in Servlet. Spring 引导应用程序带有一个内置的 Servlet。 You are probably already using this feature when launching the application inside your IDE.在 IDE 中启动应用程序时,您可能已经在使用此功能。

This basically means that you can just run your.jar file on any web server and it will be ready to go without setting up an extra tomcat instance. This basically means that you can just run your.jar file on any web server and it will be ready to go without setting up an extra tomcat instance.

However, if you want to build a Spring Boot application as a war file and deploy it to an external tomcat, you need to follow some extra steps as explained in this article .但是,如果您想将 Spring 引导应用程序构建为 war 文件并将其部署到外部 tomcat,则需要按照本文中的说明执行一些额外的步骤。

Assuming from what you posted so far: the path that is returned shows another route before your actual controller route "/demo-0.0.1-SNAPSHOT/hello" is this "/demo-0.0.1-SNAPSHOT" the path that your application runs on?假设从您到目前为止发布的内容:返回的路径在您的实际 controller 路线“/demo-0.0.1-SNAPSHOT/hello”之前显示另一条路线是这个“/demo-0.0.1-SNAPSHOT”您的应用程序的路径继续运行? If not it should be included in your controller (assuming you havent set it elsewhere for eg in your application.properties).如果不是,它应该包含在您的 controller 中(假设您没有在其他地方设置它,例如在您的 application.properties 中)。 for eg http://localhost:8080/ would be the basepath and either http://localhost:8080/demo-0.0.1-SNAPSHOT/hello or http://localhost:8080/hello would point to your controller. for eg http://localhost:8080/ would be the basepath and either http://localhost:8080/demo-0.0.1-SNAPSHOT/hello or http://localhost:8080/hello would point to your controller. Also your startup logs (for Tomcat and Spring) might give away more about the issue.此外,您的启动日志(对于 Tomcat 和 Spring)可能会泄露有关该问题的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM