You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Next »

Jackson XML

Jackson XML is a Data Format which uses the Jackson library with the XMLMapper extension to unmarshal an XML payload into Java objects or to marshal Java objects into an XML payload.

from("activemq:My.Queue").
  unmarshal().jacksonxml().
  to("mqseries:Another.Queue");

Using Jackson XML in Spring DSL

When using Data Format in Spring DSL you need to declare the data formats first. This is done in the DataFormats XML tag.

        <dataFormats>
            <!-- here we define a Xml data format with the id jack and that it should use the TestPojo as the class type when
                 doing unmarshal. The unmarshalTypeName is optional, if not provided Camel will use a Map as the type -->
            <jacksonxml id="jack" unmarshalTypeName="org.apache.camel.component.jacksonxml.TestPojo"/>
        </dataFormats>

And then you can refer to this id in the route:

       <route>
            <from uri="direct:back"/>
            <unmarshal ref="jack"/>
            <to uri="mock:reverse"/>
        </route>

Excluding POJO fields from marshalling

When marshalling a POJO to JSON you might want to exclude certain fields from the JSON output. With Jackson you can use JSON views to accomplish this. First create one or more marker classes.

Error formatting macro: snippet: java.lang.NullPointerException
Use the marker classes with the @JsonView annotation to include/exclude certain fields. The annotation also works on getters.
Error formatting macro: snippet: java.lang.NullPointerException
Finally use the Camel JacksonXMLDataFormat to marshall the above POJO to XML.
Error formatting macro: snippet: java.lang.NullPointerException
Note that the weight field is missing in the resulting JSON:

{"age":30, "weight":70}

Dependencies

To use Jackson XML in your camel routes you need to add the dependency on camel-jacksonxml which implements this data format.

If you use maven you could just add the following to your pom.xml, substituting the version number for the latest & greatest release (see the download page for the latest versions).

<dependency>
  <groupId>org.apache.camel</groupId>
  <artifactId>camel-jacksonxml</artifactId>
  <version>x.x.x</version>
  <!-- use the same version as your Camel core version -->
</dependency>
  • No labels