Hari R
Hari R

Reputation: 143

Could not unmarshal using jaxb in camel. Not invoking processor

In my route, I am trying to unmarshal an incoming XML message. However, when I execute the code, it skips to execute the .processor, and I am unable to figure out the actual error (since it's not giving one).

The code:

package nl.hari.local.cust;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.model.dataformat.JaxbDataFormat;
public class XMLtoObject_Camel {
    public static void main(String[] args) throws Exception {
        CamelContext context = new DefaultCamelContext();
        JaxbDataFormat jaxbDataFormat = new JaxbDataFormat();
        JAXBContext con = JAXBContext.newInstance(Address.class);   
        context.addRoutes(new RouteBuilder() {
            @Override
            public void configure() throws Exception {
            from("file://C:/Hari/TstFolder/"
                    + "?noop=true" + "&autoCreate=false" + "&flatten=false" + "&delete=false"
                    + "&bufferSize=128")
            .unmarshal(jaxbDataFormat)
//Doesn't invoke processor - start
            .process(new Processor(){
                public void process(Exchange exchange) throws Exception {
                    Address add = (Address) exchange.getIn().getBody();
                    System.out.println("city is" + add.getCity());
                }
            });
//Doesn't invoke processor - End
            }
        });
    }
}

This is the schema I am using. The XML I use to test is generated in Eclipse and so are the JAXB classes.

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" 
        targetNamespace="http://www.hari.nl/Address" 
        xmlns:tns="http://www.cimt.nl/Address" 
        elementFormDefault="qualified">
  <element name="address">
    <complexType>
      <sequence>
        <element type="string" name="city"/>
        <element type="string" name="country"/>
      </sequence>
      <attribute type="byte" name="id"/>
    </complexType>
  </element>
</schema>

The link below contains screen grab of project structure https://i.sstatic.net/4IWhD.png

Upvotes: 2

Views: 2356

Answers (1)

Pratiyush Kumar Singh
Pratiyush Kumar Singh

Reputation: 1997

You should use something like below code snippet

            ClassLoader cl = ObjectFactory.class.getClassLoader();
            JAXBContext jc = JAXBContext.newInstance(SomeGeneratedClass.class.getPackage().getName(), cl);
            JaxbDataFormat jaxb = new JaxbDataFormat(jc);
            jaxb.setPartClass(SomeGeneratedClass.class.getName());

Also, before unmarshaling convert it to String.

.convertBodyTo(String.class)
.unmarshal(jaxb)

Final Code should look like below.

package nl.hari.local.cust;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;

import org.apache.camel.ExchangePattern;
import org.apache.camel.LoggingLevel;
import org.apache.camel.ValidationException;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.converter.jaxb.JaxbDataFormat;

import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.impl.DefaultCamelContext;

import nl.hari.local.jaxb.Address;
import nl.hari.local.jaxb.ObjectFactory;

public class Camel_JAXB_UnMarshal {

    public static void main(String[] args) throws Exception {
        CamelContext context = new DefaultCamelContext();
        try {

            ClassLoader cl = ObjectFactory.class.getClassLoader();
            JAXBContext jc = JAXBContext.newInstance(Address.class.getPackage().getName(), cl);
            final JaxbDataFormat jaxb = new JaxbDataFormat(jc);
            jaxb.setPartClass(Address.class.getName());

            context.addRoutes(new RouteBuilder() {
                @Override
                public void configure() throws Exception {
                    from("file:resources"). //This can be changed as per ur requirement.
                    convertBodyTo(String.class)
                    .unmarshal(jaxb)
                    .process(new Processor() {

                        public void process(Exchange exchange) throws Exception {
                            Address add = (Address) exchange.getIn().getBody();
                            System.out.println("city is" + add.getCity());

                        }

                    });
                }
            });
            context.start();
            Thread.sleep(2000);
        } finally {
            context.stop();
        }
    }
}

And Eclipse Directory Structure.

enter image description here

Upvotes: 1

Related Questions