【问题标题】:SpringBoot how to get full URL of applicationSpring Boot如何获取应用程序的完整URL
【发布时间】:2022-08-05 22:21:18
【问题描述】:

基本上,我想记录在我的 SpringBoot REST API 生命周期中发生的所有事情,并且我想记录类似App started at [ip]:[port]/[everything else] 的内容

我已经看到过这样的问题,但它使用的是嵌入式 Tomcat,我使用另一个 Web 服务器,可以做到吗?这会很酷。

    标签: spring-boot logging


    【解决方案1】:

    您可以使用 Controller 中的 ServletUriComponentsBuilder 检索这些信息:

    URI currentUri = ServletUriComponentsBuilder.fromCurrentRequestUri()
                    .build()
                    .toUri();
    String asString = currentUri.toString(); // "http://localhost:8080/orders/1/items/18"
    String host = currentUri.getHost(); // "localhost"
    int port = currentUri.getPort(); // 8080
    String path = currentUri.getPath(); // "/orders/1/items/18"
    

    【讨论】:

      猜你喜欢
      • 2018-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-28
      • 2016-09-26
      • 2019-07-05
      相关资源
      最近更新 更多