我是 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/