sanjbh
sanjbh

Reputation: 65

Getting ClassCastException in Camel route while handling response containing java.util.List

I have a Camel route exposed as a CXF web service. This is a bottom up web service and has an operation like so:

List<Book> getBooks();

The CXF endpoint is defined as:

<cxf:cxfEndpoint id="bookService" 
                 address="http://localhost:9045/bookservice" 
                 serviceClass="org.test.cxfws.service.BookDBService">        
</cxf:cxfEndpoint>

The operation queries a list of books and returns it to the caller. The Camel route looks like this:

<camel:camelContext xmlns="http://camel.apache.org/schema/spring">
    <route>
        <from uri="cxf:bean:bookService"/>
        <choice>                
            <when>                    
                <simple>${header.operationName} == 'getBooks'</simple>                    
                <to uri="bean:wsImplBean?method=getBooks"/>
            </when>
            <to uri="log:outboundSoapResponse"/>
         <choice>
     </route>
</camel:camelContext>

After running the route, I am getting the following exception:

org.apache.cxf.interceptor.Fault: org.test.cxfws.service.Book cannot be cast to java.util.List at org.apache.cxf.jaxws.interceptors.WrapperClassOutInterceptor.handleMessage(WrapperClassOutInterceptor.java:117) at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:272) at org.apache.cxf.interceptor.OutgoingChainInterceptor.handleMessage(OutgoingChainInterceptor.java:77) at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:272) ...

Caused by: java.lang.ClassCastException: org.test.cxfws.service.Book cannot be cast to java.util.List at org.test.cxfws.service.GetBooksResponse_WrapperTypeHelper1.createWrapperObject(Unknown Source) at org.apache.cxf.jaxws.interceptors.WrapperClassOutInterceptor.handleMessage(WrapperClassOutInterceptor.java:101)

I can see that the getBooks method from the bean wsImpBean is executed and the result being returned at the end of the choice block inside the route:

[ qtp1653072092-14] outboundSoapResponse INFO Exchange[ExchangePattern: InOut, BodyType: java.util.ArrayList, Body: [org.test.cxfws.service.Book@63f1858b, org.test.cxfws.service.Book@5769bf0, org.test.cxfws.service.Book@2df7ac5d, org.test.cxfws.service.Book@5f55253e, org.test.cxfws.service.Book@4f003a57]]

Can someone help me to understand why the ClassCastException.

Thanks.

Upvotes: 1

Views: 3280

Answers (1)

Willem Jiang
Willem Jiang

Reputation: 3291

As camel-cxf use list to hold the response for handling the InOut parameters. When you set the response result into the message body, you need to wrap the result into a List just like this

List<Book> books ... 
List<Object> resultList = new ArrayList<Object>(); 
resultList.add(books); 
exchange.getOut().setBody(resultList); 

Upvotes: 1

Related Questions