Re: [spray-user] [spray-json] Question on a problem serializing a map of headers.

227 views
Skip to first unread message

Johannes Rudolph

unread,
Feb 11, 2013, 3:54:07 AM2/11/13
to spray...@googlegroups.com
Hi,

you have to

import spray.json._

to get the `.toJson` method.

HTH
Johannes


On Mon, Feb 11, 2013 at 4:11 AM, <kra...@gmail.com> wrote:
Greetings,

I am trying to use spray to serialize a map via JSON and it wont compile even. The toJson method is not even on the map of headers or the map[string, string] that I created. Can anyone give me a hint at what I am doing wrong? Thanks in advance and the file is below.

package com.kraythensoft.ecplat

import akka.event.Logging
import akka.camel.{CamelExtension, Consumer, CamelMessage}
import org.apache.camel.component.file.FileComponent
import spray.can.server.SprayCanHttpServerApp
import spray.json.DefaultJsonProtocol._
import scala.concurrent.duration._
import akka.actor._
import spray.util._
import spray.http._

class ReplyData(val name: String)

/** A general actor that will respond to simple text messages. */
class CamelFileActor extends Consumer {
  val log = Logging(context.system, this)

  /** TODO Configure this with some property, maybe mix in zookeeper */
  def endpointUri = "file:target/data/inbox"

  // old jetty uri "jetty:http://localhost:8877/ecplat"

  def receive = {
    case msg: CamelMessage => {
      log.info("Detected new file: ");
      val headers = msg.getHeaders
      log.info("Headers are: " + headers.toString)
      log.info("Body is: %s" format msg.bodyAs[String])
    }
  }
}

class HTTPRoutingActor extends Actor with SprayActorLogging {
  val jsonContentType = new ContentType(MediaTypes.`application/json`, Option(HttpCharsets.`UTF-8`))

  def receive = {
    case HttpRequest(HttpMethods.GET, "/ecplat", headers, _, _) =>
      val headerValues = Map[String, String](headers map { h => (h.name, h.value) }: _*)
      val hdrJSON = headerValues.toJson.prettyPrint.stripMargin
      //val hdrJSON = headers.toJson.prettyPrint.stripMargin
      val response = HttpResponse(
        entity = HttpBody(jsonContentType, hdrJSON)
      )
      sender ! response
    case _: HttpRequest => sender ! HttpResponse(status = StatusCodes.NotFound, entity = "Unknown resource!")
  }
}

/** Application that runs the server. */
object Main extends App with SprayCanHttpServerApp {
  // Start the Akka Main Actor System
  override val system = ActorSystem("Ecplat")

  // Start up the Spray related HTTP Server and related actors
  val httpServerActor = system.actorOf(Props[HTTPRoutingActor], name = "httpRoutingActor")
  httpServerActor ! Bind(interface = "localhost", port = 8080)

  // Start up the Apache Camel system and related actors
  val camel = CamelExtension(system)
  camel.context.addComponent("file", new FileComponent)
  val camelFileActor = system.actorOf(Props[CamelFileActor], name = "CamelFileActor")
  val activationFuture = camel.activationFutureFor(camelFileActor)(timeout = 10 seconds, executor = system.dispatcher)
}


--
You received this message because you are subscribed to the Google Groups "spray-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to spray-user+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Johannes

-----------------------------------------------
Johannes Rudolph
http://virtual-void.net

kra...@gmail.com

unread,
Feb 11, 2013, 9:33:34 AM2/11/13
to spray...@googlegroups.com, johannes...@googlemail.com
Hmm, thats it? I will try that. You dont see anything else wrong with it?

Thanks for the help.

kra...@gmail.com

unread,
Feb 12, 2013, 12:06:00 AM2/12/13
to spray...@googlegroups.com, johannes...@googlemail.com
Alas that didnt work. it wont compile. I get :

/Users/rsimmons/KraythenSoft/ecplat/src/main/scala/com/kraythensoft/ecplat/Actors.scala:41: value toJson is not a member of scala.collection.immutable.Map[String,String]
      val hdrJSON = headerValues.toJson.prettyPrint.stripMargin

Any idea hoe to fix that?

-- Robert



On Monday, February 11, 2013 1:54:07 AM UTC-7, Johannes Rudolph wrote:

kra...@gmail.com

unread,
Feb 12, 2013, 12:25:19 AM2/12/13
to spray...@googlegroups.com, johannes...@googlemail.com
Incidentally here is the link to the project on bitbucket.

https://bitbucket.org/kraythensoft/ecplat

 Thanks in advance.

On Monday, February 11, 2013 1:54:07 AM UTC-7, Johannes Rudolph wrote:

Johannes Rudolph

unread,
Feb 12, 2013, 5:32:28 AM2/12/13
to kra...@gmail.com, spray...@googlegroups.com
Hi Robert,

On Tue, Feb 12, 2013 at 6:25 AM, <kra...@gmail.com> wrote:
Incidentally here is the link to the project on bitbucket.

https://bitbucket.org/kraythensoft/ecplat

There's a conflict between two imports (spray.util._ and spray.json._) which both provide some extension to arbitrary data types.

I filed an issue to track this problem:


The workaround is to remove the `spray.util._` import. I fixed that and another issue in your code and published it here:

kra...@gmail.com

unread,
Feb 12, 2013, 9:27:53 AM2/12/13
to spray...@googlegroups.com
Ahh, nice. Appreciate it. Can you create a pull request? It would be fun to have someone else's name in the project. The entire project is about learning spray, akka, play and the scala stack. I am trying to have a project that users can look at and watch a ground up development.

-- robert

Reply all
Reply to author
Forward
0 new messages