【问题标题】:How to perform a simple json post with spray-json in spray?如何在 spray 中使用 spray-json 执行简单的 json 发布?
【发布时间】:2015-10-25 19:42:55
【问题描述】:

我正在尝试使用喷雾执行一个简单的 json 帖子。但似乎我可以获得一个可以是 Marshall 的 json 对象的 http 实体。

这是我的错误:

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

[错误] 验证请求 = Post(s"$thesaurusapiEndpoint/$coreProjectId/suggestFreeConcept?", 建议JsonBody)

及其附带的代码:

 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 会附带隐式编组器。

【问题讨论】:

    标签: scala spray spray-json


    【解决方案1】:

    我假设您已经在某处拥有自定义 Json 协议,以便 suggestion.toJson 正常工作?

    尝试以下方法:

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

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

    【讨论】:

      【解决方案2】:

      我是这样解决的:

      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._ 导入 spray.httpx.SprayJsonSupport._

      验证请求 = Post(s"$thesaurusapiEndpoint/$coreProjectId/suggestFreeConcept", 元帅(建议))

      我整理建议。解释不是超级简单。但是通过在文档中获取,它被解释了。

      【讨论】:

        猜你喜欢
        • 2015-04-16
        • 2013-12-29
        • 2015-09-26
        • 1970-01-01
        • 1970-01-01
        • 2015-11-21
        • 2016-07-22
        • 2018-03-18
        • 2018-11-18
        相关资源
        最近更新 更多