4 Replies Latest reply on Apr 20, 2012 7:35 AM by raul.kripalani

    Unmarshal JSON to XML using Spring DSL

    smalson

      I would like to use Jettison to transform basic JSON messages into XML and then apply validation on the XML.  Using Spring DSL how do I get Jettison to parse the JSON into XML without trying to map it to POJO's. 

       

      Here is the Camel Route:

      -


       

       

        <log message="Unmarshalled XML Body: $"/>

         

       

      Example input JSON:

      -


       

      {"Book":{"name":"CXF in Action ","id":"123"}}

       

      Get the following error indicating it is trying to map to a POJO.

      -


       

      com.thoughtworks.xstream.mapper.CannotResolveClassException: Book

           at com.thoughtworks.xstream.mapper.DefaultMapper.realClass(DefaultMapper.java:56)

       

      Does the dataFormat have to be configured to use the JettisonMappedXmlDriver and can it be done using the Spring DSL approach?

       

      Thanks

       

      Steve

        • 1. Re: Unmarshal JSON to XML using Spring DSL
          davsclaus

          A side note.

          A new component is coming in Camel 2.10, that can map directly between XML <-> JSon, camel-xmljson

           

          Currently its in development, and we dont have any documentation yet. The source code is here:

          https://svn.apache.org/repos/asf/camel/trunk/components/camel-xmljson/

           

          The json data format uses XStream, that uses Jettison. So I guess the XStream project has better documentation:

          http://xstream.codehaus.org/index.html

           

          The Camel XML DSL has a  data format that have options for configuring XStream. I wonder if any of those can help to configure it to map JSon to XML without any POJOs

          • 2. Re: Unmarshal JSON to XML using Spring DSL
            smalson

            I was able to create a Bean which wrapped the Jettison library and use it as a Transform.  That worked with the exception that it does not handle JSON arrays the way I expected.  I built the camel-xmljson project as a Bundle and deployed in ServiceMix4.4.1, but I need to know how to include the dependencies.  I get the following error:

             

            Camel :: XML JSON (0.9.0)

            karaf@root> bundles:start 188

            org.osgi.framework.BundleException: Unresolved constraint in bundle camel-xmljson : Unable to re

            solve 188.1: missing requirement package; (package=net.sf.json)

             

            I am relatively new to ServiceMix and would like to know the best way to include the dependencies that are not in ServiceMix (i.e. json-lib.jar).  I did notice there is also a patch for Camel that includes the camel-xmljson.  Would it be a better option to go about applying the diff to camel 2.8.4 release?

            • 3. Re: Unmarshal JSON to XML using Spring DSL
              ffang

              Hi,

               

              Basically FUSE ESB/Servicemix is also an OSGi container, so you can install json-lib.jar as a bundle in FUSE ESB before you install camel-xmljson bundle.

               

              Freeman

              • 4. Re: Unmarshal JSON to XML using Spring DSL
                raul.kripalani

                Hi smaison,

                 

                Typically you would install the camel-xmljson ServiceMix feature. A feature is a group of bundles, other features and configuration entries. By installing the feature, ServiceMix will take care of installing whatever is needed to make the component work.

                 

                We have provided a camel-xmljson feature which you can install using the following command:

                 

                features:install camel-xmljson/2.10-SNAPSHOT

                 

                Previously, you should have added the features repository by using the following command:

                 

                features:addurl mvn:org.apache.camel.karaf/apache-camel/2.10-SNAPSHOT/xml/features

                 

                (provided that your SMX instance is configured to access public SNAPSHOT repositories, which it is if you're running the default config)

                 

                However, I do not recommend doing this at this point, as it will drag along camel-core 2.10, camel-spring 2.10, etc. etc. etc. So an entire Camel 2.10 runtime will be installed along with any dependencies. This could mess you things for you. Instead, you probably want to make the component work with whatever version of Camel you're on.

                 

                So instead, run the following sequence of commands to install the dependencies one-by-one:

                 

                install -s mvn:org.apache.servicemix.bundles/org.apache.servicemix.bundles.commons-beanutils/1.8.3_1

                install -s mvn:org.apache.servicemix.bundles/org.apache.servicemix.bundles.oro/2.0.8_6

                install -s mvn:org.apache.servicemix.bundles/org.apache.servicemix.bundles.ezmorph/1.0.6_1-SNAPSHOT

                install -s mvn:org.apache.servicemix.bundles/org.apache.servicemix.bundles.json-lib/2.4_1-SNAPSHOT

                 

                And, finally:

                 

                install -s mvn:org.apache.camel/camel-xmljson/2.10-SNAPSHOT

                 

                And you should be good to go! (no need to build camel-xmljson locally even... as the SNAPSHOT is already available in the public Maven repos!)

                 

                Hope that helps.

                 

                Regards,

                Raúl.