如何在喷雾中使用喷雾json执行简单的json柱?

时间:2015-08-03 15:55:47

标签: scala spray spray-json

我试图用喷雾做一个简单的json帖子。但似乎我可以获得一个可以成为Marshall的json对象的http实体。

这是我的错误:

  

[错误]   ..... / IdeaProjects / PoolpartyConnector / SRC /主/阶/组织/ IADB / poolpartyconnector / thesaurusoperation / ThesaurusCacheService.scala:172:   找不到类型证据参数的隐含值   spray.httpx.marshalling.Marshaller [spray.json.JsValue]

     

[error] val request =   交的(S" $ thesaurusapiEndpoint / $ coreProjectId / suggestFreeConcept&#34 ;,   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

    }
  }

请问,是否有任何人知道隐式编组器发生了什么。我虽然喷Json会带来隐式编组。

2 个答案:

答案 0 :(得分:1)

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

尝试以下方法:

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

你也可以使用compactPrint而不是prettyPrint,在任何一种情况下,它都会将Json变成包含json信息的字符串。

答案 1 :(得分:0)

以下是我如何解决它:

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

      }

    }

  }

关键是:

  

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

  

val request =   交的(S" $ thesaurusapiEndpoint / $ coreProjectId / suggestFreeConcept&#34 ;,   编组(建议))

我提出了建议。解释不是超级直接的。但是通过在文档中找到它,它被解释了。