title: Camel Processing JSON Messages from RabbitMQ
body: <p>I want to post a message in JSON format to RabbitMQ and have that message consumed successfully. I'm attempting to use Camel to integrate producers and consumers. However, I'm struggling to understand how to create a route to make this happen. I'm using JSON Schema to define the interface between the Producer and Consumer. My application creates JSON, converts it to a byte[] and a Camel ProducerTemplate is used to send the message to RabbitMQ. On the consumer end, the byte[] message needs to be converted to a String, then to JSON, and then marshalled to an Object so I can process it. The following code line doesn't work however</p>
  
  <pre><code>from(startEndpoint).transform(body().convertToString()).marshal().json(JsonLibrary.Jackson, classOf[Payload]).bean(classOf[JsonBeanExample]), 
  </code></pre>
  
  <p>It's as if the bean is passed the original byte[] content and not the object created by JSON <code>json(JsonLibrary.Jackson, classOf[Payload])</code>. All the camel examples I've seen which use the json(..) call seem be followed by a to(..) which is the end of the route? Here is the error message</p>
  
  <pre><code>Caused by: org.apache.camel.InvalidPayloadException: No body available of type: uk.co.techneurons.messaging.Payload but has value: [B@48898819 of type: byte[] on: Message: "{\"id\":1}". Caused by: No type converter available to convert from type: byte[] to the required type:     uk.co.techneurons.messaging.Payload with value [B@48898819. Exchange[ID-Tonys-    iMac-local-54996-1446407983661-0-2][Message: "{\"id\":1}"]. Caused by:     [org.apache.camel.NoTypeConversionAvailableException - No type converter available to convert from type: byte[] to the required type: uk.co.techneurons.messaging.Payload with value [B@48898819]`    
  </code></pre>
  
  <p>I don't really want to use Spring, Annotations etc, would like to service activation as simple as possible.  Use Camel as much as possible</p>
  
  <p>This is the producer</p>
  
  <pre><code>package uk.co.techneurons.messaging
  import org.apache.camel.builder.RouteBuilder
  import org.apache.camel.impl.DefaultCamelContext
  
  object RabbitMQProducer extends App {
     val camelContext = new DefaultCamelContext
     val rabbitMQEndpoint: String = "rabbitmq:localhost:5672/advert?autoAck=false&amp;threadPoolSize=1&amp;username=guest&amp;password=guest&amp;exchangeType=topic&amp;autoDelete=false&amp;declare=false"
     val rabbitMQRouteBuilder = new RouteBuilder() {
       override def configure(): Unit = {
         from("direct:start").to(rabbitMQEndpoint)
       }
     }
     camelContext.addRoutes(rabbitMQRouteBuilder)
     camelContext.start
     val producerTemplate = camelContext.createProducerTemplate
     producerTemplate.setDefaultEndpointUri("direct:start")
     producerTemplate.sendBodyAndHeader("{\"id\":1}","rabbitmq.ROUTING_KEY","advert.edited")
    camelContext.stop
  }
  </code></pre>
  
  <p>This is the consumer..</p>
  
  <pre><code>package uk.co.techneurons.messaging
  import org.apache.camel.builder.RouteBuilder
  import org.apache.camel.impl.DefaultCamelContext
  import org.apache.camel.model.dataformat.JsonLibrary
  
  object RabbitMQConsumer extends App {
    val camelContext = new DefaultCamelContext
    val startEndpoint = "rabbitmq:localhost:5672/advert?queue=es_index&amp;exchangeType=topic&amp;autoDelete=false&amp;declare=false&amp;autoAck=false"
    val consumer = camelContext.createConsumerTemplate
    val routeBuilder = new RouteBuilder() {
      override def configure(): Unit = {
          from(startEndpoint).transform(body().convertToString()).marshal().json(JsonLibrary.Jackson, classOf[Payload]).bean(classOf[JsonBeanExample])
      }
   }
   camelContext.addRoutes(routeBuilder)
   camelContext.start
   Thread.sleep(1000)
   camelContext.stop
  }
  
  case class Payload(id: Long)
  
  class JsonBeanExample {
     def process(payload: Payload): Unit = {
       println(s"JSON ${payload}")
     }
  }
  </code></pre>
  
  <p>For completeness, this is the sbt file for easy replication..</p>
  
  <pre><code>name := """camel-scala"""
  
  version := "1.0"
  
  scalaVersion := "2.11.7"
  
  libraryDependencies ++= {
   val scalaTestVersion = "2.2.4"
   val camelVersion: String = "2.16.0"
   val rabbitVersion: String = "3.5.6"
   val slf4jVersion: String = "1.7.12"
   val logbackVersion: String = "1.1.3"
   Seq(
     "org.scala-lang.modules" %% "scala-xml" % "1.0.3",
     "org.apache.camel" % "camel-core" % camelVersion,
     "org.apache.camel" % "camel-jackson" % camelVersion,
     "org.apache.camel" % "camel-scala" % camelVersion,
     "org.apache.camel" % "camel-rabbitmq" % camelVersion,
     "com.rabbitmq" % "amqp-client" % rabbitVersion,
     "org.slf4j" % "slf4j-api" % slf4jVersion,
     "ch.qos.logback" % "logback-classic" % logbackVersion,
     "org.apache.camel" % "camel-test" % camelVersion % "test",
     "org.scalatest" %% "scalatest" % scalaTestVersion % "test")
  }
  </code></pre>
  
  <p>Thanks</p>