Return 来自 Cassandra (Akka HTTP) 的有效 JSON 数据

Return valid JSON data from Cassandra (Akka HTTP)

尽管 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)

您需要将 row => row.getFormattedContents() 更改为 row => row.getString(0) 以仅提取包含由 Cassandra 格式化的 JSON 的字段,而不是在您执行 [= 时返回格式化为 CQL 的字符串12=].