Camel不想在java DSL路由中打印POJO

时间:2017-09-21 09:47:43

标签: java xml xml-parsing jaxb apache-camel

我试图使用Apache Camel将XML解析为POJO,并且在打印出实际的POJO时遇到问题。相反,我得到常规XML,好像没有转换没有发生。当我通过客户向客户介绍其工作正常时。同时打印Customers类以完美地播出bean。

MyRoute
   @Autowired
    private MyBean mb;
    @Override
    public void configure() throws Exception {
        from("file:{{customer.path}}?noop=true")
                .bean(mb)
                .to("stream:out");
    }


MyBean

    @Handler
    public Customers whatIsInBody(Customers body) {
        return body;
    }

POJO课程:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "customer"
})
@XmlRootElement(name = "customers")
public @Data
class Customers {

    @XmlElement(required = true, nillable = true)
    protected List<Customer> customer;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "customer", propOrder = {
    "id",
    "name",
    "adress",
    "countryCode",
    "products"
})
public @Data
class Customer {

    protected long id;
    @XmlElement(required = true)
    protected String name;
    @XmlElement(required = true)
    protected String adress;
    @XmlElement(required = true)
    protected String countryCode;
    @XmlElement(required = true, nillable = true)
    protected List<Product> products;
}

输出示例:

<customers>
    <customer>
        <id>12345</id>
        <name>str1234</name>
        <adress>str1234</adress>
        <countryCode>str1234</countryCode>
        <products>
            <id>12345</id>
            <name>str1234</name>
        </products>
    </customer>

期望的输出:

Customers(customer=[Customer(id=12345, name=str1234, adress=str1234, countryCode=str1234, products=[Product(id=12345, name=str1234)]),

1 个答案:

答案 0 :(得分:0)

当你在类路径上有camel-jaxb时,带有JAXB注释的POJO类在将它转换为String类型时会被转换为XML,这就是stream:out所能做的。

您可以启用跟踪器以查看路由期间邮件包含的内容 http://camel.apache.org/tracer

因此,如果你真的想要流式传输:消息体的toString,你需要首先手动转换它,方法是调用它的toString方法

.transform(simple("${body.toString()}"))