Use freemarker in the Apache camel framework for data conversion

Source: Internet
Author: User
Tags apache camel

During system integration, an essential task is to convert data from one format to another, and then send the converted format to the target system, here we will introduce how to use freemarker in camel for data conversion.

1. The freemarker template is as follows:

<?xml version="1.0" encoding="UTF-8"?><people  xmlns:h="http://www.w3.org/TR/html4/">    <#escape x as x?xml>    <#list body.peopleList as p>    <person id="000001" age="20">        <name>            <family>${p.fname}</family>            <given>${p.gname}</given>        </name>        <email>${p.email}</email>        <link manager="${p.manager}" />        <#if p.level == "L1">        <l1tag>xxx</l1tag>        </#if>    </person>    </#list>    </#escape></people>

2. The corresponding Java object is as follows: each person node corresponds to a valueobject placed in the peoplelist of xmltemplateparameter.

public class XMLTemplateParameter {    private String fileName;        private List<ValueObject> peopleList = new ArrayList<ValueObject>();    public List<ValueObject> getPeopleList() {        return peopleList;    }    public void setPeopleList(List<ValueObject> peopleList) {        this.peopleList = peopleList;    }    public String getFileName() {        return fileName;    }    public void setFileName(String fileName) {        this.fileName = fileName;    }}public class ValueObject {    private String fname;    private String gname;    private String email;    private String manager;    private String level;

3. The route code is as follows:

public class CamelFreemarkerRoute extends RouteBuilder {    public void configure() throws Exception {        from("quartz://report?cron=10 * * * * ?&stateful=true")        .beanRef("fmBean","prepareFMValues")         .to("freemarker:com/test/camel/freemarker/test.ftl")        .to("file:d:/temp/outbox?fileName=fm.xml");        }}

4. The beans used in route are as follows: xmltemplateparameter is placed in the body as a top-level object, and the body of the data in freemarker. peoplelist corresponds to xmltemplateparameter. peoplelist.

public class FmProcessorBean {        public void prepareFMValues(Exchange exchange){        XMLTemplateParameter xmlTemplateParameter = new XMLTemplateParameter();        ValueObject val = null;        for(int i=0;i<3;i++){            val = new ValueObject();                    val.setFname("Yao");            val.setGname("Yorker" +i);            val.setEmail("test@mail.com");            val.setManager("m&an<ager");            val.setLevel("L" + i);            xmlTemplateParameter.getPeopleList().add(val);                    }                exchange.getIn().setBody(xmlTemplateParameter);    }    }

5. The spring configuration file is as follows:

<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-3.0.xsd http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd" default-autowire="byName" default-init-method="init">    <bean id="fmBean" class="com.test.camel.freemarker.FmProcessorBean"/>    <camelContext id="testCamelContext" xmlns="http://camel.apache.org/schema/spring">        <package>com.test.camel.freemarker</package>    </camelContext>    </beans>

6. Start spring. In the D: \ temp \ outbox folder, an FM. xml file is generated every 10 seconds according to the freemarker template.

Applicationcontext AC = new classpathxmlapplicationcontext ("config/camelfreemarker. xml ");
While (true ){
Thread. Sleep (2000 );
}

Explanations for this example beanref ("fmbean", "preparefmvalues:

This means that the preparefmvalues method of fmbean is called. Camel is responsible for binding the message body to the first parameter of the method to be called, and corresponding type conversion may be performed. (In this example, the first parameter of the method is exchange, and there is no conversion process). Here we give an example diagram to explain the process of binding and conversion: camel sets the exchange input message (exchange. convert getin () to string and bind it to the name parameter of MTD method. (Image Source: camel
In action)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.