【问题标题】:Spring Boot Actuator 2.0.0 ShutDown endpoint returns error 415 when invoked with POST requestSpring Boot Actuator 2.0.0 ShutDown 端点在使用 POST 请求调用时返回错误 415
【发布时间】:2018-07-25 05:51:50
【问题描述】:

我正在使用 spring-boot-starter-actuator:2.0.0.RELEASE 并启用了以下功能:

management.endpoint.shutdown.enabled=true
management.endpoints.web.exposure.include=shutdown
management.security.basic.enabled=false

通过 PowerShell 命令,我调用了 shutdown 端点:

$endpointUrl = "http://localhost:8200/actuator/shutdown"
Invoke-WebRequest -Uri $endPointUrl -Method POST

我得到了:

Invoke-WebRequest : The remote server returned an error: (415).
At line:1 char:1
+ Invoke-WebRequest -Uri $endPointUrl -Method POST
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebExc
   eption
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

在我的WebSecurityConfigurerAdapter 中,我有:

http
    .authorizeRequests()
    .csrf()
    .disable();

我的 Web 请求调用出了什么问题?

【问题讨论】:

  • 错误 415 是“不支持的媒体类型”。我想难怪你正在做一个没有正文的 POST
  • @Theo 我尝试了一个身体并得到了相同的415。执行器端点shutdown 只能通过 POST 调用。我认为它不需要任何参数
  • 我只能找到确实使用了 POST 的 cUrl 命令,但也找不到 -X 参数。但是,我还在 StackOverflow 上找到了这个答案,它可能会回答你的问题:stackoverflow.com/questions/48192765/…
  • @Theo 我试过了,它实际上在 Linux 环境中工作......但是在 Windows 命令行上,它出现了这个错误......而且,这只发生在版本 2.0.0+,以前我在另一个项目中使用了1.5+,我可以用上面的PowerShell脚本很好地关闭
  • hummmm.. 那时我想我不能帮你。 -X 似乎将 curl 命令的行为从 POST 更改为其他内容。 -i 告诉它在输出中包含标头,因此在 Powershell 中您可能需要执行类似 Invoke-WebRequest -Uri $endPointUrl -Method Head 的操作??

标签: powershell spring-boot spring-boot-actuator


【解决方案1】:

Invoke-WebRequest 中的默认 ContentType 为 application/x-www-form-urlencoded

见:https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-webrequest?view=powershell-6

-内容类型 指定 Web 请求的内容类型。如果省略此参数且请求方法为 POST,则 Invoke-WebRequest 将内容类型设置为 application/x-www-form-urlencoded。否则,调用中未指定内容类型。

我在 Postman 中尝试过,shutdown 端点与 application/json 一起工作,或者根本没有 ContentType 标头(在 Spring 文档中没有找到这个)。但看起来将此标头设置为application/json (https://github.com/PowerShell/PowerShell/issues/3131) 可能存在一些问题 所以可能只是尝试设置-ContentType 'application/json' -Body "null"。我没有 Windows 机器可以试用。

【讨论】:

  • 嘿,添加-ContentType 'application/json' 有效!它不需要-Body "null"。非常感谢
猜你喜欢
  • 2020-10-26
  • 2021-12-04
  • 2020-11-03
  • 2020-02-18
  • 2023-04-04
  • 1970-01-01
  • 2018-12-02
  • 2016-12-15
  • 1970-01-01
相关资源
最近更新 更多