【问题标题】:Spring Web Reactive not working as war file deployed on Tomcat 8.5Spring Web Reactive 不能作为部署在 Tomcat 8.5 上的 war 文件工作
【发布时间】:2018-03-24 16:45:34
【问题描述】:

我有提供一些 REST API 的 Spring Boot (2.0.0 M5) 应用程序。我想使用RouterFunctions 来实现这个 API。

当我使用嵌入式 Jetty 运行应用程序时,一切正常。

当我将应用程序转换为 WAR 文件(遵循 documentation here)并将其部署到 Tomcat 8.5 时,尝试调用任何端点时总是得到 404

我可以在日志中看到端点被识别:

[ost-startStop-1] s.w.r.r.m.a.RequestMappingHandlerMapping : Mapped "{[/say-hello],methods=[GET]}" onto java.lang.String com.example.demo.DemoController.test()
[ost-startStop-1] o.s.w.r.f.s.s.RouterFunctionMapping: Mapped /api => {
    /v1 => {
       /status -> 
          com.example.demo.DemoApplication$$Lambda$189/1904651750@5fdc83e
   }
}

但是当调用curl -v http://localhost:8080/demo/say-hellocurl -v http://localhost:8080/demo/api/v1/status 时,我看到了默认的Tomcat 404 页面。路径是正确的,我在部署之前将.war重命名为demo.war

有人遇到过类似的问题吗?您可以找到代码here

【问题讨论】:

  • 有什么错误吗?你检查过 Tomcat 控制台是否有错误?
  • 是的,我做到了。没有错误。 :-(

标签: java spring-boot spring-webflux


【解决方案1】:

恐怕目前 Spring Boot for WebFlux 不支持 WAR 部署模型。 Spring Framework 确实支持该模型(尽管我不确定它是否支持将应用程序部署到非根上下文)。

您始终可以在the Spring Boot issue tracker 上创建问题,但我不确定这是否会实现,因为部署到 Servlet 容器不是那里的主要焦点(而且您不能使用 Netty 做到这一点)。

快速说明:添加 @EnableWebFlux 表示您希望自己掌握 WebFlux 配置,并且 Spring Boot 不应在该空间为您自动配置内容。

【讨论】:

  • 如果不是 WAR,还有哪些其他可能的部署方式?
  • 一个带有嵌入式服务器的可执行 JAR。
【解决方案2】:

现在回答它,以防其他人仍然面临这个问题。

在pom文件中,需要排除netty,单独添加tomcat。

compile ('org.springframework.boot:spring-boot-starter-webflux') {
    exclude group: 'org.springframework.boot', module: 'spring-boot-starter- 
    reactor-netty'
}
compile 'org.springframework.boot:spring-boot-starter-tomcat'

完整代码请查看链接:https://github.com/sushantkr16/Spring-boot2-webflux-annotation

【讨论】:

    猜你喜欢
    • 2014-04-19
    • 2020-10-23
    • 2015-09-03
    • 2019-05-17
    • 2014-11-11
    • 1970-01-01
    • 2010-09-13
    • 2012-07-29
    • 1970-01-01
    相关资源
    最近更新 更多