Re: POJO binding with the Rest DSL
Hi Its the client code you write to send the Person using http4. It does not know how to convert that to json. You need to convert it to json first, and then send that instead of the Person java object. On Fri, Nov 21, 2014 at 5:56 PM, Tim Dudgeon wrote: > 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 > -- Claus Ibsen - Red Hat, Inc. Email: cib...@redhat.com Twitter: davsclaus Blog: http://davsclaus.com Author of Camel in Action: http://www.manning.com/ibsen hawtio: http://hawt.io/ fabric8: http://fabric8.io/
Re: POJO binding with the Rest DSL
OK, that figures, and I got it to work. But it seems to circumvent the whole purpose of the POJO binding with rest DSL. How are you supposed to send POJO to Rest DSL within Camel. I briefly tried using restlet like this: t.requestBody('restlet:http://$BASE_URL/foo?restletMethod=POST', p) but that didn't work. Thanks Tim On 21/11/2014 17:12, Claus Ibsen wrote: Hi Its the client code you write to send the Person using http4. It does not know how to convert that to json. You need to convert it to json first, and then send that instead of the Person java object. On Fri, Nov 21, 2014 at 5:56 PM, Tim Dudgeon wrote: 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
Re: POJO binding with the Rest DSL
Hi The rest DSL is on the other side, eg on the server side, to exposes Camel routes as REST services. Not on the client side how to call another REST service. For POJO -> Json you can use jackson to turn that into json. On Fri, Nov 21, 2014 at 6:41 PM, Tim Dudgeon wrote: > OK, that figures, and I got it to work. > But it seems to circumvent the whole purpose of the POJO binding with rest > DSL. > How are you supposed to send POJO to Rest DSL within Camel. > I briefly tried using restlet like this: > > t.requestBody('restlet:http://$BASE_URL/foo?restletMethod=POST', p) > > but that didn't work. > > Thanks > Tim > > > > > > On 21/11/2014 17:12, Claus Ibsen wrote: >> >> Hi >> >> Its the client code you write to send the Person using http4. It does >> not know how to convert that to json. You need to convert it to json >> first, and then send that instead of the Person java object. >> >> On Fri, Nov 21, 2014 at 5:56 PM, Tim Dudgeon >> wrote: >>> >>> 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.examp
Re: POJO binding with the Rest DSL
Yes, I understand that, and got it working that way. I had misunderstood where the POJO binding fitted in. Thanks for your help. Tim On 22/11/2014 09:27, Claus Ibsen wrote: Hi The rest DSL is on the other side, eg on the server side, to exposes Camel routes as REST services. Not on the client side how to call another REST service. For POJO -> Json you can use jackson to turn that into json. On Fri, Nov 21, 2014 at 6:41 PM, Tim Dudgeon wrote: OK, that figures, and I got it to work. But it seems to circumvent the whole purpose of the POJO binding with rest DSL. How are you supposed to send POJO to Rest DSL within Camel. I briefly tried using restlet like this: t.requestBody('restlet:http://$BASE_URL/foo?restletMethod=POST', p) but that didn't work. Thanks Tim On 21/11/2014 17:12, Claus Ibsen wrote: Hi Its the client code you write to send the Person using http4. It does not know how to convert that to json. You need to convert it to json first, and then send that instead of the Person java object. On Fri, Nov 21, 2014 at 5:56 PM, Tim Dudgeon wrote: 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 d