日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

spray.json_如何使用Spray-json(Un)在Akka HTTP中封送JSON

發(fā)布時間:2023/11/29 javascript 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spray.json_如何使用Spray-json(Un)在Akka HTTP中封送JSON 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

spray.json

by Miguel Lopez

由Miguel Lopez

如何使用Spray-json(Un)在Akka HTTP中封送JSON (How to (Un)marshal JSON in Akka HTTP with spray-json)

In the previous post, we added JSON support to our Akka HTTP API using circe.

在上一篇文章中 ,我們使用circe將JSON支持添加到了Akka HTTP API中。

This time we’ll do the same but using spray-json. Akka HTTP supports it by providing an official library — we don’t need a third-party party one like we did with circe.

這次我們將執(zhí)行相同的操作,但使用spray-json。 Akka HTTP通過提供一個官方庫來支持它-我們不需要像circe那樣的第三方。

項目設(shè)置 (Project setup)

We’ll go through the same steps as the previous tutorial to set up the project.

我們將按照與上一教程相同的步驟來設(shè)置項目。

Clone the repo, and check out the branch 3.3-repository-implementation.

克隆回購 ,并檢查了分支3.3-repository-implementation 。

We will also do the changes we did in the previous tutorial.

我們還將進(jìn)行上一教程中所做的更改。

First, we will replace the circe dependencies with the spray-json dependency since we won’t be needing it for this tutorial. Update the build.sbt file with the following contents:

首先,我們將用circle-json依賴關(guān)系替換circe依賴關(guān)系,因為本教程將不需要它。 使用以下內(nèi)容更新build.sbt文件:

name := "akkahttp-quickstart"version := "0.1"scalaVersion := "2.12.6"val akkaVersion = "2.5.13"val akkaHttpVersion = "10.1.3"libraryDependencies ++= Seq( "com.typesafe.akka" %% "akka-actor" % akkaVersion, "com.typesafe.akka" %% "akka-testkit" % akkaVersion % Test, "com.typesafe.akka" %% "akka-stream" % akkaVersion, "com.typesafe.akka" %% "akka-stream-testkit" % akkaVersion % Test, "com.typesafe.akka" %% "akka-http" % akkaHttpVersion, "com.typesafe.akka" %% "akka-http-testkit" % akkaHttpVersion % Test, "com.typesafe.akka" %% "akka-http-spray-json" % akkaHttpVersion, "org.scalatest" %% "scalatest" % "3.0.5" % Test)

Next, we will add a save function to the TodoRepository and its implementation:

接下來,我們將save函數(shù)添加到TodoRepository及其實(shí)現(xiàn)中:

import scala.concurrent.{ExecutionContext, Future}trait TodoRepository { def all(): Future[Seq[Todo]] def done(): Future[Seq[Todo]] def pending(): Future[Seq[Todo]] def save(todo: Todo): Future[Todo]}class InMemoryTodoRepository(initialTodos: Seq[Todo] = Seq.empty)(implicit ec: ExecutionContext) extends TodoRepository { private var todos: Vector[Todo] = initialTodos.toVector override def all(): Future[Seq[Todo]] = Future.successful(todos) override def done(): Future[Seq[Todo]] = Future.successful(todos.filter(_.done)) override def pending(): Future[Seq[Todo]] = Future.successful(todos.filterNot(_.done)) override def save(todo: Todo): Future[Todo] = Future.successful { todos = todos :+ todo todo }}

This will allow us to create a POST request to create new todos.

這將允許我們創(chuàng)建一個POST請求來創(chuàng)建新的待辦事項。

And finally, update the Main object to create a list of todos for testing purposes, and with the appropriate routes:

最后,更新Main對象以創(chuàng)建用于測試目的的待辦事項列表,并使用適當(dāng)?shù)穆酚?#xff1a;

import akka.actor.ActorSystemimport akka.http.scaladsl.Httpimport akka.stream.ActorMaterializerimport scala.concurrent.Awaitimport scala.util.{Failure, Success}object Main extends App { val host = "0.0.0.0" val port = 9000 implicit val system: ActorSystem = ActorSystem(name = "todoapi") implicit val materializer: ActorMaterializer = ActorMaterializer() import system.dispatcher val todos = Seq( Todo("1", "Record amazing gifs for the tutorials", "", done = false), Todo("2", "Finish the spray-json tutorial", "", done = true), ) val todoRepository = new InMemoryTodoRepository(todos) import akka.http.scaladsl.server.Directives._ def route = path("todos") { get { complete(todoRepository.all()) } ~ post { entity(as[Todo]) { todo => complete(todoRepository.save(todo)) } } } val binding = Http().bindAndHandle(route, host, port) binding.onComplete { case Success(_) => println("Success!") case Failure(error) => println(s"Failed: ${error.getMessage}") } import scala.concurrent.duration._ Await.result(binding, 3.seconds)}

With this in place, we can now move to support JSON parsing.

有了這個,我們現(xiàn)在可以支持JSON解析了。

創(chuàng)建格式 (Creating the format)

The project shouldn’t be compiling right now because Akka HTTP doesn’t know how to convert JSON to our models and vice versa.

該項目不應(yīng)立即編譯,因為Akka HTTP不知道如何將JSON轉(zhuǎn)換為我們的模型,反之亦然。

Adding JSON support with circe was quite simple. It only involved adding a couple of import statements.

使用circe添加JSON支持非常簡單。 它只涉及添加幾個導(dǎo)入語句。

Sadly, with spray-json that isn’t the case. The effort isn’t that great either.

可悲的是,使用Spray-json并非如此。 努力也不是很好。

So, let’s start.

所以,讓我們開始吧。

Because we want to use spray-json with Akka HTTP, we can look at the Akka HTTP’s official docs on how to accomplish what we want.

因為我們要在Akka HTTP上使用spray-json,所以我們可以查看Akka HTTP的官方文檔 ,了解如何完成我們想要的。

We need to extend the trait SprayJsonSupport to let Akka HTTP know how to parse our models to and from JSON (via the FromEntityUnmarshaller and ToEntityMarshaller provided by the trait).

我們需要擴(kuò)展特征SprayJsonSupport以使Akka HTTP知道如何與JSON解析模型(通過ToEntityMarshaller提供的FromEntityUnmarshaller和ToEntityMarshaller )。

And to create the actual format, we will use the trait DefaultJsonProtocol from spray-json.

為了創(chuàng)建實(shí)際的格式 ,我們將使用spray-json中的特征DefaultJsonProtocol 。

Add the following object below the Todo model:

在Todo模型下面添加以下對象:

object TodoFormat extends SprayJsonSupport with DefaultJsonProtocol { implicit val todoFormat = jsonFormat4(Todo)}

This is the extra step we need when using spray-json. It has to be done for every model we have.

這是使用spray-json時需要的額外步驟。 必須為我們擁有的每個模型完成此操作。

To get our project working, we need to import TodoFormat in our Main object:

為了使我們的項目正常工作,我們需要在我們的Main對象中導(dǎo)入TodoFormat :

import TodoFormat._import akka.http.scaladsl.server.Directives._def route = path("todos") { get { complete(todoRepository.all()) } ~ post { entity(as[Todo]) { todo => complete(todoRepository.save(todo)) } }}

Run the application and it should be working fine.

運(yùn)行該應(yīng)用程序,它應(yīng)該可以正常工作。

Let’s make some tests!

讓我們做一些測試!

測試我們的API (Testing our API)

We need to make sure our API is working as intended. So let’s query it as we did in the previous tutorial to check the functionality is the same.

我們需要確保我們的API能夠按預(yù)期工作。 因此,讓我們像上一教程中那樣查詢它,以檢查功能是否相同。

Sending a GET request to localhost:9000/todos should give us the initial todos:

發(fā)送GET請求到localhost:9000/todos應(yīng)該給我們初始的待辦事項:

Great, that works the same.

太好了,工作原理是一樣的。

Let’s see if sending invalid JSON gives us something similar:

讓我們看看是否發(fā)送無效的JSON給我們帶來了類似的效果:

It does. The error message is different but we get the same 400 Bad Request which is the important part.

是的 錯誤消息是不同的,但我們得到相同的400 Bad Request ,這是重要的部分。

Let’s create a new todo with valid JSON:

讓我們使用有效的JSON創(chuàng)建一個新的待辦事項:

And to finish off, let’s query the todos again to make sure it was saved:

最后,讓我們再次查詢待辦事項以確保已保存:

There we go. We have a working application with spray-json.

好了 我們有一個使用Spray-json的應(yīng)用程序。

Cool, isn’t it?

不錯,不是嗎?

結(jié)語 (Wrapping up)

Even though working with spray-json involves some extra manual work, you don’t need an additional third-party dependency to get it to work with Akka HTTP.

即使使用spray-json涉及一些額外的手動工作,您也不需要額外的第三方依賴關(guān)系即可使其與Akka HTTP一起使用。

It’s a matter of preference really.

這確實(shí)是一個優(yōu)先事項。

In the future, we will explore how to accomplish different use cases with both to compare them. So stay tuned!

將來,我們將探索如何完成不同的用例并將它們進(jìn)行比較。 敬請期待!

If you liked this tutorial and wanted to learn how to build an API for a todo application, check out our new free course! ???

如果您喜歡本教程,并且想學(xué)習(xí)如何為待辦事項應(yīng)用程序構(gòu)建API,請查看我們的新免費(fèi)課程! ???

Akka HTTP QuickstartLearn how to create web applications and APIs with Akka HTTP in this free course!link.codemunity.io

Akka HTTP快速入門 在此免費(fèi)課程中,了解如何使用Akka HTTP創(chuàng)建Web應(yīng)用程序和API! link.codemunity.io

Originally published at www.codemunity.io.

最初在www.codemunity.io上發(fā)布。

翻譯自: https://www.freecodecamp.org/news/how-to-un-marshal-json-in-akka-http-with-spray-json-1407876373a7/

spray.json

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎

總結(jié)

以上是生活随笔為你收集整理的spray.json_如何使用Spray-json(Un)在Akka HTTP中封送JSON的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。