【问题标题】:Akka http unsupported method exceptionAkka http 不支持的方法异常
【发布时间】:2026-01-30 17:05:01
【问题描述】:

我有一个这样定义的普通 http 路由

delete {
           handleErrorsAndReport("delete_foo") {
              fooRepository.deleteFoo(fooId)
              complete(NoContent)
           }
}

我想为其添加一些基本身份验证,所以现在看起来像

seal(
            authenticateBasic(
              realm = "Secure foo",
              scopeAuthenticator
            ) { scopes =>
              delete {
                handleErrorsAndReport("delete_foo") {
                  fooRepository.deleteFoo(fooId, scopes)
                  complete(NoContent)
                }
              }
            }
)

完整的指令是

concat(
  get {
  // something else which is working
  },
  seal(
  // something else which is working
  ),
  seal(
              authenticateBasic(
                realm = "Secure foo",
                scopeAuthenticator
              ) { scopes =>
                delete {
                  handleErrorsAndReport("delete_foo") {
                    fooRepository.deleteFoo(fooId, scopes)
                    complete(NoContent)
                  }
                }
              }
  )
)

现在我在尝试删除 foos 时遇到以下异常

 Request DELETE http://builder/foos/fooId failed with response code 405 due to request error. Response body: HTTP method not allowed, supported methods: PUT

可能是什么问题?我使用 API 的方式没有改变,但恐怕随着 seal 指令的引入而发生了一些变化。

【问题讨论】:

    标签: http akka


    【解决方案1】:

    我不知道为什么,但这有效

    concat(
      get {
      // something else which is working
      },
      seal(
                  authenticateBasic(
                    realm = "Secure foo",
                    scopeAuthenticator
                  ) { scopes =>
                    concat(
                       delete {
                         handleErrorsAndReport("delete_foo") {
                           fooRepository.deleteFoo(fooId, scopes)
                           complete(NoContent)
                         },
                         put {//other authenticated endpoint}
                    )
                    }
                  }
      )
    )
    

    换句话说,我合并了之前分开的两个密封指令。可能和this有关

    【讨论】: