代码之家  ›  专栏  ›  技术社区  ›  JasonG

Akka HTTP、Spray Json和选项字段按要求失败

  •  2
  • JasonG  · 技术社区  · 6 年前

    我有一门案例课:

    case class OpcTagPlaybook(name: String, tags: Seq[OpcTagAtTime], looped: Boolean, enabled: Option[String])
    

    带有可选的启用字段,该字段将包含日期。 Spray Json描述了编码/解码:

    implicit val opcTagPlaybookFormat = jsonFormat4(OpcTagPlaybook)
    

    以及akka http中的一条路由:

    post {
      entity(as[OpcTagPlaybook]) { playbook =>
        val playbookCreated: Future[_] =
          (opcTagPlaybookActor ? UpsertPlaybook(playbook))
        onSuccess(playbookCreated) { _ =>
          log.info("Created playbook [{}]", playbook.name)
          complete((StatusCodes.Created, "ok"))
        }
      }
    })
    

    由于某些原因,在没有createdAt字段的情况下过账失败:

    邮寄:

    {"name": "test1",
    "tags":[{"offset": 100, "tag": "hello", "value": "yo"}],
    "looped": true
    }
    

    失败:

    The request content was malformed:
    Object is missing required member 'enabled'
    

    如果我发帖,它会给我另一个错误:

     {"name": "test1",
     "tags":[{"offset": 100, "tag": "hello", "value": "yo"}],
     "looped": true,
     "enabled": "2018-08-08"
     }
    

    给予

    The request content was malformed:
    spray.json.JsString cannot be cast to scala.Option
    

    我假设这是akka http之间的一些相互作用,所以我将更手动地处理输入,但我只是想了解我在这里做错了什么。

    0 回复  |  直到 6 年前