Scala 如何在spray中使用spray json执行简单的json post?

Scala 如何在spray中使用spray json执行简单的json post?,scala,spray,spray-json,Scala,Spray,Spray Json,我正在尝试使用spray执行一个简单的json post。但我似乎可以为json对象获取一个http实体,该实体可以是Marshall 这是我的错误: [错误] ..…/IdeaProjects/PoolpartyConnector/src/main/scala/org/iadb/PoolpartyConnector/thesaurusoperation/ThesaurusCacheService.scala:172: 找不到类型为的证据参数的隐式值 spray.httpx.marshallin

我正在尝试使用spray执行一个简单的json post。但我似乎可以为json对象获取一个http实体,该实体可以是Marshall

这是我的错误:

[错误] ..…/IdeaProjects/PoolpartyConnector/src/main/scala/org/iadb/PoolpartyConnector/thesaurusoperation/ThesaurusCacheService.scala:172: 找不到类型为的证据参数的隐式值 spray.httpx.marshalling.Marshaller[spray.json.JsValue]

[错误]val请求= 发布“$thesaurusapiEndpoint/$CoreProjected/suggestFreeConcept?”, suggestionJsonBody)

以及随附的代码:

 override def createSuggestedFreeConcept(suggestedPrefLabel: String, lang: String, scheme: String, b: Boolean): String = {

    import system.dispatcher
    import spray.json._

    val pipeline      = addCredentials(BasicHttpCredentials("superadmin", "poolparty")) ~> sendReceive


    val label              = LanguageLiteral(suggestedPrefLabel, lang)
    val suggestion         = SuggestFreeConcept(List(label), b, Some(List(scheme)), None, None,None, None)
    val suggestionJsonBody = suggestion.toJson

    val request            = Post(s"$thesaurusapiEndpoint/$coreProjectId/suggestFreeConcept?", suggestionJsonBody)

    val res                = pipeline(request)

    getSuggestedFromFutureHttpResponse(res) match {

      case None => ""
      case Some(e) => e

    }
  }

拜托,有人知道马歇尔是怎么回事吗。我认为spray Json将随隐式封送器一起提供。

我假设您已经在某个地方有了自定义Json协议,以便
suggestion.toJson
能够正常工作

请尝试以下操作:

val body = HttpEntity(`application/json`, suggestionJsonBody.prettyPrint)
val request = Post(s"$thesaurusapiEndpoint/$coreProjectId/suggestFreeConcept?", body)

您也可以使用
compactPrint
而不是
prettyPrint
,在这两种情况下,它都会将Json转换为包含Json信息的字符串。

以下是我如何解决它的:

override def createSuggestedFreeConcepts(suggestedPrefLabels: List[LanguageLiteral], scheme: String, checkDuplicates: Boolean): List[String] = {


    import system.dispatcher

    import spray.httpx.marshalling._
    import spray.httpx.SprayJsonSupport._

    val pipeline      = addCredentials(BasicHttpCredentials("superadmin", "poolparty")) ~> sendReceive


    suggestedPrefLabels map { suggestedPrefLabel =>

      val suggestion    = SuggestFreeConcept(List(suggestedPrefLabel), checkDuplicates, Some(List(Uri(scheme))), None, None, None, None)
      val request       = Post(s"$thesaurusapiEndpoint/$coreProjectId/suggestFreeConcept", marshal(suggestion))

      val res           = pipeline(request)

      getSuggestedFromFutureHttpResponse(res) match {

        case None => ""
        case Some(e) => e

      }

    }

  }
关键是:

导入spray.httpx.marshalling.\uu导入spray.httpx.SprayJsonSupport_

val请求= 发布“$thesaurusapiEndpoint/$CoreProjected/suggestFreeConcept”, 元帅(建议))

我同意你的建议。这一解释并不十分直截了当。但是,通过在文档中访问,可以解释这一点