I'm having problems getting POJO binding working with the Rest DSL.
I put together a simple example in Groovy, but it fails:
package com.im.examples.search
import org.apache.camel.CamelContext
import org.apache.camel.ProducerTemplate
import org.apache.camel.builder.RouteBuilder
import org.apache.camel.impl.DefaultCamelContext
import org.apache.camel.model.rest.RestBindingMode
import org.apache.camel.model.dataformat.JsonLibrary
String HOST = 'localhost'
String PORT = '43256'
String BASE_URL = "$HOST:$PORT/search"
class Person {
String firstName
String lastName
}
CamelContext camelContext = new DefaultCamelContext()
camelContext.addRoutes(new RouteBuilder() {
def void configure() {
restConfiguration().component("restlet").host(HOST).port(PORT).bindingMode(RestBindingMode.json)
rest("/search")
.post("/foo").type(Person.class).to("direct:end")
from('direct:end')
.log('${body}')
.transform().constant('Welcome')
from('direct:testmarshal')
.marshal().json(JsonLibrary.Jackson)
.log('JSON: ${body}')
}
})
camelContext.start()
Person p = new Person(firstName: 'John', lastName: 'Doe')
ProducerTemplate t = camelContext.createProducerTemplate()
t.sendBody('direct:testmarshal', p)
def resp = t.requestBody("http4:$BASE_URL/foo", p)
println "resp: $resp"
camelContext.stop()
The error I get is:
Caused by: org.apache.camel.InvalidPayloadException: No body available
of type: java.io.InputStream but has value:
com.im.examples.search.Person@6c4b8217 of type:
com.im.examples.search.Person on: Message:
com.im.examples.search.Person@6c4b8217. Caused by: No type converter
available to convert from type: com.im.examples.search.Person to the
required type: java.io.InputStream with value
com.im.examples.search.Person@6c4b8217. Exchange[Message:
com.im.examples.search.Person@6c4b8217]. Caused by:
[org.apache.camel.NoTypeConversionAvailableException - No type converter
available to convert from type: com.im.examples.search.Person to the
required type: java.io.InputStream with value
com.im.examples.search.Person@6c4b8217]
at
org.apache.camel.impl.MessageSupport.getMandatoryBody(MessageSupport.java:101)
at
org.apache.camel.component.http4.HttpProducer.createRequestEntity(HttpProducer.java:461)
at
org.apache.camel.component.http4.HttpProducer.createMethod(HttpProducer.java:368)
at
org.apache.camel.component.http4.HttpProducer.process(HttpProducer.java:106)
at
org.apache.camel.util.AsyncProcessorConverterHelper$ProcessorToAsyncProcessorBridge.process(AsyncProcessorConverterHelper.java:61)
at
org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:191)
at
org.apache.camel.processor.UnitOfWorkProducer.process(UnitOfWorkProducer.java:74)
at
org.apache.camel.impl.ProducerCache$2.doInProducer(ProducerCache.java:375)
at
org.apache.camel.impl.ProducerCache$2.doInProducer(ProducerCache.java:343)
at org.apache.camel.impl.ProducerCache.doInProducer(ProducerCache.java:233)
at org.apache.camel.impl.ProducerCache.sendExchange(ProducerCache.java:343)
at org.apache.camel.impl.ProducerCache.send(ProducerCache.java:201)
at
org.apache.camel.impl.DefaultProducerTemplate.send(DefaultProducerTemplate.java:128)
at
org.apache.camel.impl.DefaultProducerTemplate.sendBody(DefaultProducerTemplate.java:132)
... 28 more
Caused by: org.apache.camel.NoTypeConversionAvailableException: No type
converter available to convert from type: com.im.examples.search.Person
to the required type: java.io.InputStream with value
com.im.examples.search.Person@6c4b8217
at
org.apache.camel.impl.converter.BaseTypeConverterRegistry.mandatoryConvertTo(BaseTypeConverterRegistry.java:182)
at
org.apache.camel.impl.MessageSupport.getMandatoryBody(MessageSupport.java:99)
... 41 more
I thought I had carefully followed the documentation :-(
Can anyone help spot what is wrong?
Thanks
Tim