The Camel CXF endpoint does not return POJOs
How do I make Camel cxfEndpoint
return a POJO? Currently it returns a MessageContentsList
, which contains the SOAP message fields that are members of the String
. I want the endpoint to return a POJO that is generated from wsdl using cxf-codegen-plugin
. Note that the service classes provided to the endpoint are generated using the same tools and are used.
This is the Camel context and route I’m using.
<!-- this is a spring XML file where we have Camel embedded -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:camel="http://camel.apache.org/schema/spring"
xmlns:cxf="http://camel.apache.org/schema/cxf"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.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.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml"/>
<import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
<bean class="org.apache.camel.component.cxf.transport.CamelTransportFactory" lazy-init="false">
<property name="bus" ref="cxf"/>
<property name="camelContext" ref="camelContext"/>
<property name="checkException" value="true"/>
<property name="transportIds">
<list>
<value>http://cxf.apache.org/transports/camel</value>
</list>
</property>
</bean>
<bean id="messageLoggerBean" class="tutoivon.api.camel.MessageLoggerBean" />
<bean id="messageConverterBean" class="tutoivon.api.camel.MessageConverterBean" />
<cxf:cxfEndpoint id="endpoint" address="http://localhost:1010/hello"
serviceClass="net.webservicex.GlobalWeatherSoap" wsdlURL="META-INF/globalweather.wsdl">
</cxf:cxfEndpoint>
<!-- Here we define Camel, notice the namespace it uses -->
<camelContext id="camelContext" xmlns="http://camel.apache.org/schema/spring">
<!-- Camel route to move messages from the ActiveMQ inbox to its outbox
queue -->
<route id="cxfToJMSRoute">
<from uri="cxf:bean:endpoint?dataFormat=POJO" />
<log message="test" />
<to uri="bean:messageConverterBean" />
<!-- <convertBodyTo type="String"/> -->
<wireTap uri="direct:logInfo" />
<to uri="activemq:queue:api" />
</route>
<route id="loggingRoute">
<from uri="direct:logInfo" />
<to uri="bean:messageLoggerBean" />
</route>
</camelContext>
<bean id="activemq" class="org.apache.activemq.camel.component.ActiveMQComponent">
<property name="brokerURL" value="tcp://127.0.0.1:61616"/>
</bean>
</beans>
Here is the MessageConverterBean
that processes the message.
public class MessageConverterBean implements Processor {
@Override
public void process(Exchange e) throws Exception {
GetCitiesByCountry body = e.getIn().getBody(GetCitiesByCountry.class);
System.out.println("maa: " + body.getCountryName());
e.getIn().setBody(body, GetCitiesByCountry.class);
}
}
However, the body cannot be converted to POJO because the following exception is thrown.
org.apache.camel.TypeConversionException: Error during type conversion from type: org.apache.cxf.message.MessageContentsList to the required type: net.webservicex.GetCitiesByCountry with value [Finland] due null
at org.apache.camel.converter.jaxb.FallbackTypeConverter.convertTo(FallbackTypeConverter.java:166) ~[camel-jaxb-2.20.1.jar!/:2.20.1]
at org.apache.camel.impl.converter.BaseTypeConverterRegistry.doConvertTo(BaseTypeConverterRegistry.java:366) ~[camel-core-2.20.1.jar!/:2.20.1]
at org.apache.camel.impl.converter.BaseTypeConverterRegistry.convertTo(BaseTypeConverterRegistry.java:141) ~[camel-core-2.20.1.jar!/:2.20.1]
at org.apache.camel.impl.MessageSupport.getBody(MessageSupport.java:87) ~[camel-core-2.20.1.jar!/:2.20.1]
at org.apache.camel.impl.MessageSupport.getBody(MessageSupport.java:61) ~[camel-core-2.20.1.jar!/:2.20.1]
at tutoivon.api.camel.MessageConverterBean.process(MessageConverterBean.java:12) ~[classes!/:1.0-SNAPSHOT]
at org.apache.camel.component.bean.AbstractBeanProcessor.process(AbstractBeanProcessor.java:108) ~[camel-core-2.20.1.jar!/:2.20.1]
at org.apache.camel.component.bean.BeanProcessor.process(BeanProcessor.java:53) ~[camel-core-2.20.1.jar!/:2.20.1]
at org.apache.camel.component.bean.BeanProducer.process(BeanProducer.java:41) ~[camel-core-2.20.1.jar!/:2.20.1]
at org.apache.camel.processor.SendProcessor.process(SendProcessor.java:148) ~[camel-core-2.20.1.jar!/:2.20.1]
at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:548) ~[camel-core-2.20.1.jar!/:2.20.1]
at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:201) [camel-core-2.20.1.jar!/:2.20.1]
at org.apache.camel.processor.Pipeline.process(Pipeline.java:138) [camel-core-2.20.1.jar!/:2.20.1]
at org.apache.camel.processor.Pipeline.process(Pipeline.java:101) [camel-core-2.20.1.jar!/:2.20.1]
at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:201) [camel-core-2.20.1.jar!/:2.20.1]
at org.apache.camel.component.cxf.CxfConsumer$CxfConsumerInvoker.asyncInvoke(CxfConsumer.java:203) [camel-cxf-2.20.1.jar!/:2.20.1]
at org.apache.camel.component.cxf.CxfConsumer$CxfConsumerInvoker.invoke(CxfConsumer.java:180) [camel-cxf-2.20.1.jar!/:2.20.1]
Here is the contents of the actual message payload available to the processor.
This is the message I want to send.
How do I get Camel to convert a message to a POJO? Here are all my dependencies.
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-core</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>3.2.1</version>
<exclusions>
<exclusion>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-continuation</artifactId>
</exclusion>
<exclusion>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-http</artifactId>
</exclusion>
<exclusion>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-io</artifactId>
</exclusion>
<exclusion>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-security</artifactId>
</exclusion>
<exclusion>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
</exclusion>
<exclusion>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-util</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-continuation</artifactId>
<version>9.4.6.v20170531</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-http</artifactId>
<version>9.4.6.v20170531</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-io</artifactId>
<version>9.4.6.v20170531</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-security</artifactId>
<version>9.4.6.v20170531</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>9.4.6.v20170531</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-util</artifactId>
<version>9.4.6.v20170531</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-core</artifactId>
<version>2.20.1</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-cxf</artifactId>
<version>2.20.1</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jaxb</artifactId>
<version>2.20.1</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-cxf-transport</artifactId>
<version>2.20.1</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-camel</artifactId>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-spring</artifactId>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-broker</artifactId>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-kahadb-store</artifactId>
</dependency>
</dependencies>
Solution
You don’t need CxfTransport or specify dataFormat=POJO,
as this is already the default data format. Your class should be JAXB annotated and referenced in your service class (net.webservicex.GlobalWeatherSoap).
Here’s an example of what it looks like:
National .java:
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
@XmlType(name = "GetCitiesByCountry")
@XmlAccessorType(XmlAccessType.FIELD)
public class GetCitiesByCountry {
String CountryName;
}
GlobalWeatherSoap.java:
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jwx.WebResult;
@WebService (targetNamespace="myNamespace")
public interface GlobalWeatherSoap {
@WebMethod
@WebResult(name = "GetCitiesByCountryResponse", targetNamespace = "myNamespace")
public GetCitiesByCountryResponse GetCitiesByCountry(
@WebParam(name = "GetCitiesByCountry", targetNamespace = "myNamespace")
GetCitiesByCountry country
)
}
Obviously, you replace “myNamespace” with the one used in your WSDL.