Question

camelContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Configures the Camel Context-->
    <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"
       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">
    <bean id="emp" class="dp.employee"/>
    <bean id="emp1" class="dp.employee1"/>
    <bean id="app" class="dp.Apptest"/>
    <bean id="a" class="dp.A"/>
    <bean id="t" class="dp.Test"/>
    <bean id="dozerBeanMapper" class="dp.DozerBeanMapper">
        <property name="mappingFiles">
                <list>
                    <value>../dozer-mapping.xml</value>
                </list>
        </property>
    </bean>
    <camelContext xmlns="http://camel.apache.org/schema/spring">
        <route>
            <from uri="file:src/data?noop=true"/>
            <log message="ExternalReference Value1"/>
            <setHeader headerName="name">
                <xpath resultType="java.lang.String">/employee/name</xpath>
            </setHeader>
            <setHeader headerName="address">
                <xpath resultType="java.lang.String">/employee/address</xpath>
            </setHeader>
            <log message="ExternalReference Value2"/>
            <to uri="bean:a?method=method1"/>
            <log message="Bean called"/>
            <to uri="bean:dozerBeanMapper"/>
            <log message="Dozercalled"/>
         </route>
    </camelContext>
</beans>

I am taking an xml file by setting exchange header I am getting the value in java object and printing it class A. Then I am going to map two java object using DozerBeanMapper class. I am setting the values for mappingFiles in camelContext. How to write DozerDeanMapper class for mapping using dozer.

dozer-mapping.xml:

 <?xml version="1.0" encoding="UTF-8"?>
    <mappings xmlns="http://dozer.sourceforge.net" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://dozer.sourceforge.net http://dozer.sourceforge.net/schema/beanmapping.xsd">

        <configuration>
            <stop-on-errors>true</stop-on-errors>
        </configuration>
        <mapping>
            <class-a>dp.employee</class-a>
            <class-b>dp.employee1</class-b>
            <field>
                <a>name</a>
                <b>ename</b>
            </field>
            <field>
                <a>address</a>
                <b>eaddress</b>
            </field>
        </mapping>
</mappings>

I want to know how to implement DozerBeanMapper . So that I can do mapping. Please help me how to map the two object using dozer.

Was it helpful?

Solution

You don't have to implement the DozzerBeanMapper yourself. From the Camel documentation:

<!-- the registry will be scanned and 'mapper' below will be found and installed -->
<bean id="dozerConverterLoader" class="org.apache.camel.converter.dozer.DozerTypeConverterLoader" />

<bean id="mapper" class="org.dozer.DozerBeanMapper">
    <property name="mappingFiles">
        <list>
            <value>dozer-mapping.xml</value>
        </list>
    </property>
</bean>

A typical route definition would look as follows:

<route><from uri="direct:service-in"/>
    <to uri="bean:employee-processor"/>
    <to uri="mock:verify-model"/>
</route>

<bean id="employee-processor" class="do.EmployeeProcessor"/>

The processor expects an object of type dp.Employee1:

public class EmployeeProcessor {
    public dp.Employee1 processCustomer(dp.Employee1 employee) {
        System.out.println("Processing employee "+employee.getEname());
        return employee;
    }
}

The route is started with an object of type dp.Employee:

template.sendBody("direct:service-in", new dp.Employee employee);

That way, Camel automatically converts the input object of type dp.Employee to an object of type dp.Employee1.

A full example can be found here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top