从Cassandra返回有效的JSON数据(Akka HTTP)[重复]

问题描述 投票:0回答:1

尽管在SELECT语句中使用了JSON标志,但来自Cassandra数据库的数据却以无效的JSON返回。

这是我收到的数据。

当我的前端接收到这个数据时,它当然被认为是无效的JSON。同时我也不知道为什么 "[json]" 是有的。

Future(Success(["[json]":'{"pasta_name": "conchiglie", "id": 2, "description": "description for conchiglie", "image": {"alt": "alt text", "src": "image.jpg"}}']))

这是我想收到的东西。

{
    "pasta_name": "conchiglie",
    "id": 2, "description":
    "description for conchiglie",
    "image": 
        {
            "alt": "alt text", 
            "src": "image.jpg"
        }
}

这是我的简化代码

object Server extends App {

implicit val system: ActorSystem = ActorSystem("helloworld")
implicit val executor: ExecutionContext = ExecutionContext.global
implicit val materializer: ActorMaterializer = ActorMaterializer()

val sessionSettings = CassandraSessionSettings()
implicit val cassandraSession: CassandraSession =
    CassandraSessionRegistry.get(system).sessionFor(sessionSettings)

val recipes: Future[String] =
    CassandraSource(s"SELECT JSON * FROM danlough.recipe_by_pasta").map(row => row.getFormattedContents()).runWith(Sink.head)

import akka.http.scaladsl.server.Directives._
  def route = path("getRecipe") {
    get {
      respondWithHeaders(RawHeader("Access-Control-Allow-Origin", "http://localhost:3000"), RawHeader("Vary", "Origin")) {
        complete(recipes)
      }
    }
  }

Http().bindAndHandle(route, host, port)
json scala cassandra akka akka-http
1个回答
2
投票

你需要改变 row => row.getFormattedContents()row => row.getString(0) 只提取包含Cassandra格式化的JSON的字段,而不是像您执行 row.getFormattedContents().

© www.soinside.com 2019 - 2024. All rights reserved.