Jackson XML
Available as of Camel 2.16
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.
Info |
---|
If you are familiar with Jackson, this XML data format behaves in the same way as its JSON counterpart, and thus can be used with classes annotated for JSON serialization/deserialization. This extension also mimics JAXB's "Code first" approach. This data format relies on Woodstox (especially for features like pretty printing), a fast and efficient XML processor. |
Code Block |
---|
from("activemq:My.Queue"). unmarshal().jacksonxml(). to("mqseries:Another.Queue"); |
...
When marshalling a POJO to JSON XML you might want to exclude certain fields from the JSON XML output. With Jackson you can use JSON views to accomplish this. First create one or more marker classes.
Wiki Markup |
---|
{snippet:id=marker|lang=java|url=camel/trunk/components/camel-jacksonxml/src/test/java/org/apache/camel/component/jacksonxml/Views.java} |
@JsonView
annotation to include/exclude certain fields. The annotation also works on getters.Wiki Markup |
---|
{snippet:id=jsonview|lang=java|url=camel/trunk/components/camel-jacksonxml/src/test/java/org/apache/camel/component/jacksonxml/TestPojoView.java} |
JacksonXMLDataFormat
to marshall the above POJO to XML.Wiki Markup |
---|
{snippet:id=format|lang=java|url=camel/trunk/components/camel-jacksonxml/src/test/java/org/apache/camel/component/jacksonxml/JacksonMarshalViewTest.java} |
Code Block |
---|
{"<pojo age=":30," "weight=":70}"/> |
Include/Exclude fields using the jsonView
attribute with JacksonXML
DataFormat
...
As an example of using this attribute you can instead of:
...
Code Block |
---|
<dataFormats> <jacksonxml id="jsonjacksonxml" include="NOT_NULL"/> </dataFormats> |
...
If you use jackson to unmarshal json XML to POJO, then you can now specify a header in the message that indicate which class name to unmarshal to.
The header has key CamelJacksonUnmarshalType
if that header is present in the message, then Jackson will use that as FQN for the POJO class to unmarshal the XML payload as.
...
Code Block |
---|
<dataFormats> <jacksonxml id="jsonjacksonxml" allowJmsType="true"/> </dataFormats> |
Unmarshalling from
...
XML to List<Map> or List<pojo>
If you are using Jackson to unmarshal XML to a list of map/pojo, you can now specify this by setting useList="true"
or use the org.apache.camel.component.jacksonxml.ListJacksonXMLDataFormat
. For example with Java you can do as shown below:
...
Code Block |
---|
<dataFormats> <jacksonxml id="jsonjack" useList="true"/> </dataFormats> |
And you can specify the pojo type also
Code Block |
---|
<dataFormats> <json<jacksonxml id="jsonjack" library="Jackson" useList="true" unmarshalTypeName="com.foo.MyPojo"/> </dataFormats> |
Using custom Jackson modules
Available as of Camel 2.15
You can use custom Jackson modules by specifying the class names of those using the moduleClassNames option as shown below.
Code Block |
---|
<dataFormats> <json<jacksonxml id="json" library="Jacksonjack" useList="true" unmarshalTypeName="com.foo.MyPojo" moduleClassNames="com.foo.MyModule,com.foo.MyOtherModule"/> </dataFormats> |
...
Code Block |
---|
<bean id="myJacksonModule" class="com.foo.MyModule"> ... // configure the module as you want </bean> <dataFormats> <json<jacksonxml id="json" library="Jacksonjacksonxml" useList="true" unmarshalTypeName="com.foo.MyPojo" moduleRefs="myJacksonModule"/> </dataFormats> |
...
Enabling or disable features using Jackson
Available as of Camel 2.15
Jackson has a number of features you can enable or disable, which its ObjectMapper uses. For example to disable failing on unknown properties when marshalling, you can configure this using the disableFeatures:
Code Block |
---|
<dataFormats> <json<jacksonxml id="jsonjacksonxml" library="Jackson" unmarshalTypeName="com.foo.MyPojo" disableFeatures="FAIL_ON_UNKNOWN_PROPERTIES"/> </dataFormats> |
...
Converting Maps to POJO using Jackson
Available since Camel 2.16. Jackson ObjectMapper
can be used to convert maps to POJO objects. Jackson component comes with the data converter that can be used to convert java.util.Map
instance to non-String, non-primitive and non-Number objects.
...
If there is a single ObjectMapper
instance available in the Camel registry, it will used by the converter to perform the conversion. Otherwise the default mapper will be used.
Formatted
...
XML marshalling (pretty-printing)
...
Using the prettyPrint
option one can output a well formatted JSON while XML while marshalling:
Code Block |
---|
<dataFormats> <json<jacksonxml id="xstream" prettyPrint="true"/> <json id="jackson" jack" prettyPrint="true" library="Jackson"/> <json id="gson" prettyPrint="true" library="Gson"/> </dataFormats> |
And in Java DSL:
Code Block |
---|
from("direct:inPretty").marshal().jsonjacksonxml(true); from("direct:inPretty").marshal().json(JsonLibrary.Jackson, true); from("direct:inPretty").marshal().json(JsonLibrary.Gson, true); |
Please note that as of Camel 2.16 there’re there are 5 different overloaded jsonjacksonxml()
DSL methods which support the prettyPrint
option in combination with other settings for JsonLibrary
, unmarshalType
, jsonView
etc.
...