How to make a simple Json Post with Stpray

45 views
Skip to first unread message

Maatary Okouya

unread,
Aug 3, 2015, 11:49:58 AM8/3/15
to spray.io User List
I'M GETTING THE FOLLOWING ERROR


[error] ...../IdeaProjects/PoolpartyConnector/src/main/scala/org/iadb/poolpartyconnector/thesaurusoperation/ThesaurusCacheService.scala:172: could not find implicit value for evidence parameter of type spray.httpx.marshalling.Marshaller[spray.json.JsValue]

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


For the following code: 


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

import system.dispatcher
import spray.json._

val pipeline = addCredentials(BasicHttpCredentials("admin", "admin")) ~> 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

}
 

Maatary Okouya

unread,
Aug 3, 2015, 11:50:41 AM8/3/15
to spray.io User List
How can create a json HttpEntity that comes with a marshaller ?

Age Mooij

unread,
Aug 4, 2015, 7:16:09 AM8/4/15
to spray...@googlegroups.com
If you want Marshallers for raw spray-json AST nodes (i.e. JsObject and JsArray), you need the following imports:

import spray.json.DefaultJsonProtocol._
import spray.httpx.SprayJsonSupport._

DefaultJsonProtocol contains RootJsonFormats for JsObject and JsArray while SprayJsonSupport contains Marshallers for anything that has an implicit RootJsonFormat.

If the value that you are trying to marshal is typed as JsValue (instead of as JsObject or JsArray) than marshalling will fail since JsValue cannot be proven to produce valid Json since a JSON string, boolean, etc. are not valid documents.

In general you don't need to convert to Json yourself, just supply a matching implicit RootJsonFormat and the Marshallers in SprayJsonSupport will do the rest. 

Hope this helps
Age


--
You received this message because you are subscribed to the Google Groups "spray.io User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to spray-user+...@googlegroups.com.
Visit this group at http://groups.google.com/group/spray-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/spray-user/3a272b66-a442-4512-b0f7-a7e91d4b30f8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Maatary Okouya

unread,
Apr 28, 2016, 5:50:23 AM4/28/16
to spray.io User List
Many thanks for the detail answer
Reply all
Reply to author
Forward
0 new messages