【问题标题】:How to send a response code as a response in Akka Http?如何在 Akka Http 中发送响应代码作为响应?
【发布时间】:2017-01-12 10:54:04
【问题描述】:

我有以下代码:

val route:Route={
    path("hello"){
      get{
        complete{
          "done"
        }
      }
    }
  }

  Http().bindAndHandle(route, "localhost", 8187)

这里的完整响应字符串“完成”。但是,我希望它返回 200 的状态码。我该怎么做?

【问题讨论】:

  • 它实际上返回了一个 HTTP 200 代码

标签: scala akka akka-http


【解决方案1】:

如 cmets 中所述,默认响应代码为 200,因此您得到的正是您想要的。一般来说,complete 上的the documentation 演示了如何编写带有任何状态码的complete

complete(StatusCodes.OK)

complete(StatusCodes.Create -> "message")

complete(201 -> "another message")

【讨论】:

    【解决方案2】:

    你可以使用 HttpResponse 类

     import akka.http.scaladsl.model._
    
     complete{
        HttpResponse(StatusCodes.OK, entity = "Result ok")
       //HttpResponse(StatusCodes.InternalServerError, entity = "Error") 
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-04-14
      • 2021-07-10
      • 2011-03-23
      • 1970-01-01
      • 2016-01-12
      • 2020-11-12
      相关资源
      最近更新 更多