Spring执行器即使设置了server.servlet.contextPath仍然出现404未找到错误。

huangapple go评论85阅读模式
英文:

Spring actuator giving 404 Not found error even after setting server.servlet.contextPath

问题

我有一个Spring Batch项目(使用Gradle)。我希望将该项目集成到Actuator中。

我已经在Gradle中添加了以下导入:
implementation "org.springframework.boot:spring-boot-starter-actuator"

在application.yaml中,我已经添加了以下内容:

server:
  port: 8083
  servlet:
    context-path: "/test"

现在,当我尝试在本地访问 http://localhost:8083/test/health 时,我得到错误:

{
    "timestamp": "2020-09-21T18:36:42.779+00:00",
    "status": 404,
    "error": "Not Found",
    "message": "",
    "path": "/test/health"
}

在浏览器上运行相同的端点时,我看到这个错误:

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Mon Sep 21 18:45:07 UTC 2020
There was an unexpected error (type=Not Found, status=404).

但是,当我访问其他端点,比如 http://localhost:8083/nikhil/api/boot-appliation 时,它是可以工作的!

是否有任何建议我可能遗漏了什么?

谢谢

英文:

I have a spring batch project (Using Gradle). I want to the project to be integrated with Actuator.

I have added this to import to gradle
implementation "org.springframework.boot:spring-boot-starter-actuator"

In application.yaml, I have added

server:
  port: 8083
  servlet:
    context-path: "/test"

Now, when I try to hit - http://localhost:8083/test/health on my local, I get error -

{
    "timestamp": "2020-09-21T18:36:42.779+00:00",
    "status": 404,
    "error": "Not Found",
    "message": "",
    "path": "/test/health"
}

On running the same endpoint on browser, I see this error-

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Mon Sep 21 18:45:07 UTC 2020
There was an unexpected error (type=Not Found, status=404).

But when I hit other endpoints like http://localhost:8083/nikhil/api/boot-appliation , it works!

Any suggestions where I am missing?

Thanks

答案1

得分: 1

默认情况下,上下文路径是 /,而执行器端点是:

/actuator/health

将上下文路径设置为 /test 时,执行器端点是:

/test/actuator/health

URL 中的 /actuator 部分来自于 management.endpoints.web.base-path 属性。我认为这是您想要设置的内容:

management:
  endpoints:
    web:
      base-path: /actuator

因此,综合起来,如果您的配置如下:

server:
  port: 8083
  servlet:
    context-path: /
management:
  endpoints:
    web:
      base-path: "/test"

您的执行器端点将是:

/test/health
英文:

By default the context-path is /, and the actuator endpoint is:

/actuator/health

With the context-path set to /test, the actuator endpoint is:

/test/actuator/health

The /actuator portion of the url is from management.endpoints.web.base-path property. I think this is what you meant to set:

management:
  endpoints:
    web:
      base-path: /actuator

So, all together, if your configuration is this:

server:
  port: 8083
  servlet:
    context-path: /
management:
  endpoints:
    web:
      base-path: "/test"

Your actuator endpoint will be:

/test/health

huangapple
  • 本文由 发表于 2020年9月22日 02:39:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/63998161.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定