Aaron Cirillo
Aaron Cirillo

Reputation: 65

Having trouble using data from a header in a camel route

In my camel route I am attempting to set a custom header and set the value of that header to data contained in the body. This header is later used in a SQL query, but it is not working correctly. I get an exception and it appears that the SQL query never gets the value of my header. Here is my camel route:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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/cxf http://camel.apache.org/schema/cxf/camel-cxf.xsd
    http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
<camelContext trace="false" xmlns="http://camel.apache.org/schema/spring">
<route>
    <from uri="cxf:bean:soapEndpoint"/>
    <log message="${body}"/>
    <setHeader headerName="accountNumber">
        <simple>${body}</simple>
    </setHeader>
    <log message="The header value is ${header.accountNumber}" />
    <to uri="sql:select account_name from hz_cust_accounts where account_number=:#accountNumber"/>
</route>
</camelContext>
<!-- this is the JDBC data source -->
<bean id="OracleDS" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
    <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
    <property name="url" value="jdbc:oracle:thin:@myserver:1558:mydb" />
    <property name="username" value="someuser" />
    <property name="password" value="somepass" />
</bean>

<!-- configure the Camel SQL component to use the JDBC data source -->
<bean id="sql" class="org.apache.camel.component.sql.SqlComponent">
    <property name="dataSource" ref="OracleDS" />
</bean>

<cxf:cxfEndpoint id="soapEndpoint" address="http://localhost:10001/erpsoap"
    serviceClass="apps.vci.camel.erptest.ERPSoapImpl"
    wsdlURL="META-INF/wsdl/GetHzCustDetailsService.wsdl"
    endpointName="s:getHzCustDetailsPort"
    serviceName="s:getHzCustDetailsService"
    xmlns:s="http://apps.vci.camel.erptest" />

</beans>

When the data travels through the route this is the error I get:

org.springframework.jdbc.UncategorizedSQLException: PreparedStatementCallback; uncategorized SQLException for SQL [select account_name from hz_cust_accounts where account_number=?]; SQL state [99999]; error code [17004]; Invalid column type; nested exception is java.sql.SQLException: Invalid column type

It's like the SQL component doesn't get the value that's in the header. I do log the value after I set it and I do see that it's set correctly because I get this back in my log:

[               qtp665755841-45] route1                         INFO  The header value is 4089699

Anyone have any idea why this might be happening to me?

Thanks

Upvotes: 1

Views: 3649

Answers (1)

Peter Keller
Peter Keller

Reputation: 7646

Force the header accountNumber to be an Integer:

<setHeader headerName="accountNumber">
    <simple>${bodyAs(Integer)}</simple>
</setHeader>

There are a few types which have a shorthand notation, so we can use String instead of java.lang.String. These are: byte[], String, Integer, Long. All other types must use their FQN name, e.g. org.w3c.dom.Document (Camel documentation).

Upvotes: 0

Related Questions