Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

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

Include/Exclude fields using the jsonView attribute with JacksonXMLDataFormat

Available as of Camel 2.12

As an example of using this attribute you can instead of:

Code Block
languagejava
JacksonXMLDataFormat ageViewFormat = new JacksonXMLDataFormat(TestPojoView.class, Views.Age.class);
from("direct:inPojoAgeView").
  marshal(ageViewFormat);

Directly specify your JSON view inside the Java DSL as:

Code Block
languagejava
from("direct:inPojoAgeView").
  marshal().jacksonxml(TestPojoView.class, Views.Age.class);

And the same in XML DSL:

Code Block
languagexml
<from uri="direct:inPojoAgeView"/>
  <marshal>
    <jacksonxml unmarshalTypeName="org.apache.camel.component.jacksonxml.TestPojoView" jsonView="org.apache.camel.component.jacksonxml.Views$Age"/>
  </marshal>

Setting serialization include option

If you want to marshal a pojo to XML, and the pojo has some fields with null values. And you want to skip these null values, then you need to set either an annotation on the pojo, 

Code Block
@JsonInclude(Include.NON_NULL)
public class MyPojo {
   ...
}

But this requires you to include that annotation in your pojo source code. You can also configure the Camel JacksonXMLDataFormat to set the include option, as shown below:

Code Block
JacksonXMLDataFormat format = new JacksonXMLDataFormat();
format.setInclude("NON_NULL");

Or from XML DSL you configure this as

Code Block
    <dataFormats>
      <jacksonxml id="json" include="NOT_NULL"/>
    </dataFormats>

Unmarshalling from XML to POJO with dynamic class name

If you use jackson to unmarshal json 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.

 For JMS end users there is the JMSType header from the JMS spec that indicates that also. To enable support for JMSType you would need to turn that on, on the jackson data format as shown:

Code Block
JacksonDataFormat format = new JacksonDataFormat();
format.setAllowJmsType(true);

Or from XML DSL you configure this as

Code Block
    <dataFormats>
      <jacksonxml id="json" allowJmsType="true"/>
    </dataFormats>

Unmarshalling from json 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
JacksonXMLDataFormat format = new ListJacksonXMLDataFormat();
// or
JacksonXMLDataFormat format = new JacksonXMLDataFormat();
format.useList();
// and you can specify the pojo class type also
format.setUnmarshalType(MyPojo.class);

And if you use XML DSL then you configure to use list using useList attribute as shown below:

Code Block
    <dataFormats>
      <jacksonxml id="json" useList="true"/>
    </dataFormats>

And you can specify the pojo type also

Code Block
    <dataFormats>
      <json id="json" 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 id="json" library="Jackson" useList="true" unmarshalTypeName="com.foo.MyPojo" moduleClassNames="com.foo.MyModule,com.foo.MyOtherModule"/>
    </dataFormats>

When using moduleClassNames then the custom jackson modules are not configured, by created using default constructor and used as-is. If a custom module needs any custom configuration, then an instance of the module can be created and configured, and then use modulesRefs to refer to the module as shown below:

Code Block
    <bean id="myJacksonModule" class="com.foo.MyModule">
      ... // configure the module as you want
    </bean>
 
    <dataFormats>
      <json id="json" library="Jackson" useList="true" unmarshalTypeName="com.foo.MyPojo" moduleRefs="myJacksonModule"/>
    </dataFormats>

 Multiple modules can be specified separated by comma, such as moduleRefs="myJacksonModule,myOtherModule"

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 id="json" library="Jackson" unmarshalTypeName="com.foo.MyPojo" disableFeatures="FAIL_ON_UNKNOWN_PROPERTIES"/>
 </dataFormats>

You can disable multiple features by separating the values using comma. The values for the features must be the name of the enums from Jackson from the following enum classes

  • com.fasterxml.jackson.databind.SerializationFeature
  • com.fasterxml.jackson.databind.DeserializationFeature
  • com.fasterxml.jackson.databind.MapperFeature

To enable a feature use the enableFeatures options instead.

From Java code you can use the type safe methods from camel-jackson module:

Code Block
languagejava
JacksonDataFormat df = new JacksonDataFormat(MyPojo.class);
df.disableFeature(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
df.disableFeature(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES);

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.

Code Block
languagejava
Map<String, Object> invoiceData = new HashMap<String, Object>();
invoiceData.put("netValue", 500);
producerTemplate.sendBody("direct:mapToInvoice", invoiceData);
...
// Later in the processor
Invoice invoice = exchange.getIn().getBody(Invoice.class);

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 JSON marshalling (pretty-printing)

Available as of Camel 2.16

Using the prettyPrint option one can output a well formatted JSON while marshalling:

Code Block
 <dataFormats>
      <json id="xstream" prettyPrint="true"/>
      <json id="jackson" prettyPrint="true" library="Jackson"/>
      <json id="gson" prettyPrint="true" library="Gson"/>
 </dataFormats>

And in Java DSL:

Code Block
from("direct:inPretty").marshal().json(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 5 different overloaded json() DSL methods which support the prettyPrint option in combination with other settings for JsonLibraryunmarshalTypejsonView etc. 

Dependencies

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

...