4 Replies Latest reply on Jun 16, 2011 6:55 AM by jamie archibald

    camel-jaxb unmarshalling as byte[] array?

    jamie archibald Apprentice

      I have a simple route as follows:

       

       

      String pkg = ObjectFactory.class.getPackage().getName();

      JaxbDataFormat jaxb = new JaxbDataFormat(pkg);

      jaxb.setPrettyPrint(true);

      jaxb.setPartClass("com.company.SomeClassType");

      jaxb.setPartNamespace(new QName("http://www.company.com/MyNamespace"));

      jaxb.setContextPath(pkg);

       

      from(input)

      .unmarshal(jaxb)

      .process(new MyProcessor());

      .to(output)

       

      I am sending to the "input" endpoint a serialized version of SomeClassType as an XML string.

      For some reason MyProcessor receives the SomeClassType as a byte[] array instead of the SomeTypeClass object.

       

      Any idea why it would do this?

        • 1. Re: camel-jaxb unmarshalling as byte[] array?
          Willem Jiang Master

          I can't tell any thing is wrong according to your camel route.

          Can you check if there any error log ?

          BTW, which version of camel are you using?

           

          Willem

          • 2. Re: camel-jaxb unmarshalling as byte[] array?
            jamie archibald Apprentice

            I added a few processors in my route for debugging and changed a few things:

             

            // setup JAXB, This is important otherwise the XML will not get unmarshalled properly

            //JAXBContext context = JAXBContext.newInstance(MyObject.class);

            String pkg = ObjectFactory.class.getPackage().getName();

            JaxbDataFormat jaxb = new JaxbDataFormat(pkg);

            //jaxb.setContext(context);

            jaxb.setPrettyPrint(true);

            jaxb.setPartClass(MyObject.class.getName());

            jaxb.setPartNamespace(new QName("http://www.mynamespace.com/foo"));

            jaxb.setEncoding("UTF-8");

             

            deadLetterChannel("log:$");

                       

            // consume messages from simulation

            from("activemq:queue:Foo")

            .unmarshal(jaxb) // the output of this is my object but no fields are populated

            .process(new CustomProcessor()

            .to("activemq:topic:Bar");

             

             

            I was able to get the class MyObject to unmarshal however the content is empty.

             

            For example:

             

             

             

            results in an object

             

            MyClass c;

            c.getSomeElement(); // this returns null

             

             

            One of the things I find very annoying and confusing is that JAXB is not verbose at all. I turned on debugging and I get no errors, warnings, or exceptions.

             

            Edited by: jamie3 on Jun 15, 2011 6:44 PM

            • 3. Re: camel-jaxb unmarshalling as byte[] array?
              jamie archibald Apprentice

              OK i seem to have found the issue why JAXB is creating an empty object.

               

              I wrote a unit test which sends both XML documents to the unmarshaller, for example:

               

               

               

               

              I did some debugging and it appears that the second case works. Thus the XML header must be included in order for JAXB to successfully unmarshal the string.

               

              The problem with this is I have a 3rd party system generating the XML document and publishing to my Foo queue. Unfortunately this system does not publish the XML header.

               

              Is there a way around this?

              • 4. Re: camel-jaxb unmarshalling as byte[] array?
                jamie archibald Apprentice

                Ok the JAXB generated Java object is now being populated. It looked like there was a type in the namespace.