简体   繁体   English

如何在akka-http请求中发送json对象数组

[英]How can I send array of json objects in akka-http request

I can handle {"phone": "2312323", "message": "This is test"} in Akka-Http using 我可以使用Akka-Http处理{"phone": "2312323", "message": "This is test"}

entity(as[Message]) { message =>
                  val f: Future[Any] = service ask message
                  onSuccess(f) { r: Any =>
                  {
                    r match {
                      case Some(message: Message) =>
                        complete(HttpEntity(ContentTypes.`application/json`, message.toJson.prettyPrint))
                      case _ =>
                        complete(StatusCodes.NotFound)
                    }
                  }
                  }
                }

but how can I handle 但是我该如何处理

[
     {"phone": "2312323", "message": "This is test"},
     {"phone": "2312321213", "message": "This is test 1212"}
]

Hi You can do like this: 您好您可以这样做:

import akka.http.scaladsl.server.Directives
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport
import spray.json._

// domain model
final case class Info(phone: String, message: String)
final case class MessageInfo(messages: List[Info])

// collect your json format instances into a support trait:
trait JsonSupport extends SprayJsonSupport with DefaultJsonProtocol {
  implicit val itemFormat = jsonFormat2(Info)
  implicit val orderFormat = jsonFormat1(MessageInfo) // contains List[Info]
}

// use it wherever json (un)marshalling is needed
class MyJsonService extends Directives with JsonSupport {

  // format: OFF
  val route =
    post {
      entity(as[MessageInfo]) { messageInfo => // will unmarshal JSON to Order
        val messageCount = messageInfo.messages.size
        val phoneNumbers = messageInfo.items.map(_.phone).mkString(", ")
        complete(s"Messages $messageCount with the contact number: $phoneNumbers")
      }
    }
  // format: ON
}

For more info please see here: https://doc.akka.io/docs/akka-http/current/common/json-support.html 有关更多信息,请参见此处: https : //doc.akka.io/docs/akka-http/current/common/json-support.html

You can use a simpler way, with less code (and use gson ): 您可以使用更简单的方法,减少代码(并使用gson ):

val gson = new Gson
...

entity(as[String]) { messageStr =>
              val f: Future[Any] = service ask message
              onSuccess(f) { r: Any =>
              {
                r match {
                  case Some(messageStringValue: String) =>
                    val messagesList = gson.fromJson(messageStringValue, classOf[Array[Message]])
                    val messageCount = messagesList.size
                    val phoneNumbers = messagesList.map(_.phone).mkString(", ")
    complete(s"Messages $messageCount with the contact number: $phoneNumbers")
                  case _ =>
                    complete(StatusCodes.NotFound)
                }
              }
              }
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM