如何查找SpringBoot應用中的請求路徑(不使用idea)

背景

昨天有個同事向我諮詢某個接口的物理表是哪個,由於公司業務較多、這塊業務的確不是我負責的,也沒有使用idea不能全局搜索(eclipse搜不到jar內的字符串),也就回復了不清楚。

除了自己寫代碼輸出servlet的路徑和類外,發現了一個我之前沒用過的方法:Spring Boot Actuator,分享給大家。

文中未啓用/actuator/**路徑的授權訪問功能,僅供測試環境學習。如需配置授權訪問請自行研究。

操作方式

1、引入actuator依賴

gradle

implementation "org.springframework.boot:spring-boot-starter-actuator"

maven

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

2、配置文件啓用actuator

management:
  server:
    port: ${server.port} #引用server.port,與服務同端口,也可設其他值。想禁用/actuator端口可設爲-1
  endpoints:
    web.exposure.include: "mappings" #僅暴露出路徑映射信息

3、訪問接口獲取json

訪問服務地址/actuator/mappings 會返回一個json,將該json複製到文本中,使用vscode或其他工具格式化成json。

然後就可以在json中搜索到相關的信息了。

  • contexts.應用名.mappings.dispatcherServlets.dispatcherServlet 數組裏就是當前服務通過springmvc方式暴露的接口信息了。
  • contexts.應用名.mappings.servletFilters 數組裏包含過濾器信息。
  • contexts.應用名.mappings.servlets 數組中包含原生寫法注入的servlet信息。

這裏的應用名對應spring.application.name配置的值。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章