Akka-Http 中的实体是什么?

What is an entity in Akka-Http?

我是 akka-http 的新手,在 scala 中构建一个基本的服务器-客户端应用程序。我查看的示例具有对象 "entity"。有人可以解释一下背后的概念、为什么使用它以及它有什么用吗?

post {
    path("insert") {
      entity(as[Student]) {
        obj => complete {
          insertingstudent(obj)
          s"got obj with name ${obj.getName()}"
        }
      }

谢谢

Can someone please explain the concept underlying and why is it used and how is it useful?

entity 的类型是 HttpEntity. From the comments of the code:

Models the entity (aka "body" or "content") of an HTTP message.

它是对 HTTP 请求内容的抽象。很多时候,当发送 HTTP 请求时,它们会在请求正文中提供有效负载。这个正文可以有多种格式,流行的是 JSON 和 XML.

当你写:

entity(as[Student])

您正在尝试将请求正文解组或反序列化为您喜欢的数据结构。这意味着您在后续函数中的 obj 字段的类型为 Student.