...
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. |
...
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
...
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 |
---|
<bean id="myJacksonModule" class="com.foo.MyModule"> ... // configure the module as you want </bean> <dataFormats> <jacksonxml id="jsonjacksonxml" useList="true" unmarshalTypeName="com.foo.MyPojo" moduleRefs="myJacksonModule"/> </dataFormats> |
...
Code Block |
---|
<dataFormats> <jacksonxml id="jsonjacksonxml" unmarshalTypeName="com.foo.MyPojo" disableFeatures="FAIL_ON_UNKNOWN_PROPERTIES"/> </dataFormats> |
...
Using the prettyPrint
option one can output a well formatted JSON while XML while marshalling:
Code Block |
---|
<dataFormats> <jacksonxml id="jack" prettyPrint="true"/> </dataFormats> |
...