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;
}
}