0

I'm trying to use Apache Camel to parse XML to POJO and have problem with printing out the actual POJO. Instead I get regular XML as if no convertation not happening. When I pass for example Customer insted of Customers its working okay. Also printing Customers class to sout in bean warks perfectly.

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 classes:

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

Output example:

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

Desired output:

Customers(customer=[Customer(id=12345, name=str1234, adress=str1234, countryCode=str1234, products=[Product(id=12345, name=str1234)]),
Eugene
  • 21
  • 7
  • Have you looked at Camel data formats? There are built in data formats for Jackson XML and others which can do the automatic unmarshalling for you. http://camel.apache.org/jackson-xml.html – Souciance Eqdam Rashti Sep 21 '17 at 10:26
  • @SoucianceEqdamRashti I've tried with jaxb at a seperate main - it works okay, when I try to add .unmarshal(jaxb) where jaxb is JAXBContext jc = JAXBContext.newInstance(Customers.class); DataFormat jaxb = new JaxbDataFormat(jc); but when i add it to route - nothing happens... – Eugene Sep 21 '17 at 10:40
  • Did you do like described here? https://stackoverflow.com/questions/26800182/jaxb-marshalling-in-apache-camel – Souciance Eqdam Rashti Sep 21 '17 at 10:47
  • @SoucianceEqdamRashti yes, I have ObjectFactory since I generated POJO with intellij out of xsd, and tried both ways bounded and unbounded – Eugene Sep 21 '17 at 12:22
  • Do you have camel-jaxb on the classpath? – Claus Ibsen Sep 21 '17 at 15:24
  • @ClausIbsen yes – Eugene Sep 21 '17 at 15:27

1 Answers1

0

When you have camel-jaxb on the classpath, then a POJO class with JAXB annotations is type converted to XML when you convert it to a String type, which is what the stream:out would do.

You can enable the tracer to see what the message contains during routing http://camel.apache.org/tracer

So if you really want to stream:out the toString of the message body, you would need to transform it manually first, by calling its toString method

.transform(simple("${body.toString()}"))
Claus Ibsen
  • 56,060
  • 7
  • 50
  • 65
  • So it's identical if I change signature of bean to return String and call return body.toString() ? And if the component in "to" can work with java objects there will be no conversation? – Eugene Sep 22 '17 at 07:48
  • Yes if your bean return a String and you call `return body.toString()` then the `stream:out` will use that as-is – Claus Ibsen Sep 22 '17 at 08:34
  • Can you please help with understending why this heppens? https://stackoverflow.com/questions/46447191/cant-add-application-prop-to-camel-test – Eugene Sep 29 '17 at 10:15