Ermintar
Ermintar

Reputation: 1393

Camel unmarshal Rest response exception

I'm facing an issue with unmarshalling response from Rest call

My camel-context.xml looks like:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:camel="http://camel.apache.org/schema/spring"
    xmlns:cxf="http://camel.apache.org/schema/cxf"
    xmlns:osgi="http://www.springframework.org/schema/osgi"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd       http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd       http://camel.apache.org/schema/cxf http://camel.apache.org/schema/cxf/camel-cxf-2.8.3.xsd">

    <bean class="com.myapp.MyProcessor" id="myResponseProcessor"/>

    <camelContext id="camelId" xmlns="http://camel.apache.org/schema/spring">

        <camel:route id="myServiceCreate">
            <!-- SKIPPING PREPARATION PART -->
            <log message="BODY ----- ${body}"/>
            <marshal>
                <json library="Jackson"/>
            </marshal>
            <setHeader headerName="CamelHttpMethod">
                <constant>POST</constant>
            </setHeader>
            <to uri="{{services.myuri}}/create"/>
            <log message="Message: ${body}"></log>
            <unmarshal>
                <json library="Jackson"  unmarshalTypeName="com.myapp.MyPojo"/>
            </unmarshal>
            <process id="_processMyResponse" ref="myResponseProcessor"/>
        </camel:route>
    </camelContext>
</beans>

As a result I get an exception

Caused by: com.fasterxml.jackson.databind.JsonMappingException: No content to map due to end-of-input
 at [Source: org.apache.camel.converter.stream.CachedOutputStream$WrappedInputStream@68de2df1; line: 1, column: 0]  

I've tried adding a cast to String:

  <convertBodyTo type="String"/>

but it caused exception with BufferedStream.

Logs show that a response is ok:

17:13:25.532 [http-nio-0.0.0.0-8080-exec-1] INFO  myServiceCreate - Message: {"collectionId":"123"}

How can I fix unmarshalling?

Upvotes: 3

Views: 2316

Answers (1)

Erik Karlstrand
Erik Karlstrand

Reputation: 1527

The reason unmarshalling started working after you removed the logging is because the body is of the type InputStream which means the stream will be flushed after accessing it the first time, which in this case would be the log.

If, as you say, you need to have logging then add the cast to String before logging, i.e. immediately after to.

<to uri="{{services.myuri}}/create"/>
<convertBodyTo type="java.lang.String" />
<log message="Message: ${body}"></log>
<unmarshal>
    <json library="Jackson"  unmarshalTypeName="com.myapp.MyPojo"/>
</unmarshal>

Edit

I also found this FAQ which explains this phenomenon.

Upvotes: 1

Related Questions