You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将应用路径设置为Spring Boot Actuator基础路径(@ApplicationPath)

解决Spring Boot Actuator路径访问404问题

核心配置修正

你之前修改management.endpoints.web.base-path = my-app的方向错误,这个配置是修改Actuator端点自身的根路径,而非给整个应用添加路径前缀。要实现http://localhost:8080/my-app/actuator/的访问方式,需调整以下配置:

  • 设置Spring Boot应用的上下文路径(给所有接口统一加前缀):
    server.servlet.context-path=/my-app
    
  • 保持Actuator的基础路径默认值(或显式声明):
    management.endpoints.web.base-path=/actuator
    
    配置完成后,Actuator的访问路径会自动变为http://localhost:8080/my-app/actuator/,同时应用内的其他业务接口也会带上/my-app前缀。

配合Spring网关的路由调整

由于应用部署在Spring Gateway之后,需确保网关路由规则正确转发路径前缀:

  • 在网关配置中,将/my-app/**路径的请求转发到你的Spring Boot服务,注意保留前缀(避免StripPrefix过滤掉my-app)。示例配置:
    spring:
      cloud:
        gateway:
          routes:
          - id: my-app-route
            uri: lb://my-app-service # 你的服务注册名称
            predicates:
            - Path=/my-app/**
            filters:
            - StripPrefix=0 # 完整转发路径,应用可收到/my-app/actuator这类请求
    

验证与排查

  1. 重启Spring Boot应用和网关服务
  2. 访问http://localhost:8080/my-app/actuator/health,确认返回正常健康状态
  3. 若仍报404,检查以下内容:
    • Docker Swarm的服务端口映射是否正确
    • 网关路由是否生效,可通过网关的/actuator/gateway/routes端点查看路由详情
    • 应用配置是否加载成功,可通过/my-app/actuator/env查看配置项的实际生效值

内容的提问来源于stack exchange,提问作者KingFeming

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 15:06:02