scala - Akka-Http 中的实体是什么?

标签 scala akka entity akka-http

我是 akka-http 的新手,并在 Scala 中构建了一个基本的服务器-客户端应用程序。我查看的示例具有对象“实体”。有人可以解释一下潜在的概念,为什么使用它以及它有什么用处?

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 .

关于scala - Akka-Http 中的实体是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41977406/

相关文章:

Scala 的collectFirst 函数返回Option[U]

scala - 访问由 Source.actorRef 创建的 akka 流源的底层 ActorRef

scala - Akka HTTP 背压连接

c++ - char* 类型的值不能用于初始化 "char"类型的实体

c# - 实例化实体模型时看不到 dbcontext 方法即 savechanges

scala - 为什么SBT自定义插件解析速度慢

scala - scala 的 for-comprehensions 什么时候懒惰?

scala - 在Slick中使用自定义列类型时进行过滤

java - 从 Actor 运行 CompletableFuture

java - 使用 SQL 信息填充架构文档和代码注释 使用 Hibernate 工具?