note of camel in action--chapter 3~8

3~8章  csdn有翻译.

https://blog.csdn.net/daydaylearn/article/details/52994176

第三章:

   数据格式转换, 一般有processor,bean,transform三种方式, 前两种不必要介绍了.  transform()使用方式主要如下:

  1   from("direct:start") 
  .transform(body().regexReplaceAll("\n", "<br/>")) 
  .to("mock:result");

  2  from("direct:start") 
  .transform(new Expression() { 
    public <T> T evaluate(Exchange exchange, Class<T> type) { 
    String body = exchange.getIn().getBody(String.class); 
    body = body.replaceAll("\n", "<br/>"); 
    body = "<body>" + body + "</body>"; 
    return (T) body; 
    } 
  }) 
  .to("mock:result");

  3  xml 配置

<bean id="htmlBean" class="camelinaction.HtmlBean"/> 
<camelContext id="camel" xmlns="http://camel.apache.org/schema/spring"> 
<route> 
<from uri="direct:start"/> 
<transform> 
<method bean="htmlBean" method="toHtml"/> 
</transform> 
<to uri="mock:result"/> 
</route> 
</camelContext> 
public class HtmlBean {

    public static String toHtml(String body) {
        body = body.replaceAll("\n", "<br/>");
        body = "<body>" + body + "</body>";
        return body;
    }
    
}

  4  simple 脚本语言

 

 <camelContext trace="true" id="camel" xmlns="http://camel.apache.org/schema/spring">
        <route>
            <from uri="direct:start"/>
            <transform>
                <simple>Hello ${body} how are you?</simple>
            </transform>
            <to uri="mock:result"/>
        </route>
    </camelContext>

 5 数据格式转换 
  在Camel中,数据格式转换时可插拔的数据转换,可将消息从一种形式转换为另一种形式。Camel中,每一种数据格式转换都由接口org.apace.camel.spi.DataFormat  代表,此接口包含两个方法: 
  1、marshal方法:封存一个消息为另一种形式,比如封存java对象为XML, CSV, EDI, HL7等数据模型;即对象--->二进制 
  2、unmarshal方法:进行一种反向操作,将某种格式的数据转换为消息,即二进制--->对象

 

 

  marshal举例:

      

public class PurchaseOrderJSONTest extends CamelTestSupport {

    private static Logger LOG = LoggerFactory.getLogger(PurchaseOrderJSONTest.class);

    @Override
    protected JndiRegistry createRegistry() throws Exception {
        // register our service bean in the Camel registry
        JndiRegistry jndi = super.createRegistry();
        jndi.bind("orderService", new OrderServiceBean());
        return jndi;
    }

    @Test
    public void testJSON() throws Exception {
        String out = template.requestBody("http://localhost:8080/order/service?id=123", null, String.class);
        LOG.info("Response from order service:+++++++ " + out);

        assertNotNull(out);
        assertTrue(out.contains("Camel in Action"));
    }

    @Override
    protected RouteBuilder createRouteBuilder() throws Exception {
        return new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                from("jetty://http://0.0.0.0:8080/order/service")
                    .beanRef("orderService", "lookup")
                    .marshal().json(JsonLibrary.Jackson);
            }
        };
    }

    public static class OrderServiceBean {

        public PurchaseOrder lookup(@Header("id") String id) {
            LOG.info("Finding purchase order for id " + id);
            // just return a fixed response
            PurchaseOrder order = new PurchaseOrder();
            order.setPrice(49.95);
            order.setAmount(1);
            order.setName("Camel in Action");
            return order;
        }

    }

}
@CsvRecord(separator = ",", crlf = "UNIX")
public class PurchaseOrder {

    @DataField(pos = 1)
    private String name;

    @DataField(pos = 2, precision = 2)
    private BigDecimal price;

    @DataField(pos = 3)
    private int amount;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public BigDecimal getPrice() {
        return price;
    }

    public void setPrice(BigDecimal price) {
        this.price = price;
    }

    public int getAmount() {
        return amount;
    }

    public void setAmount(int amount) {
        this.amount = amount;
    }
}

 

转载于:https://www.cnblogs.com/liuheng0213/p/10584837.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值