Versions Compared

Key

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

...

As of Camel 2.10
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.

Wiki Markup
{snippet:id=marker|lang=java|url=camel/trunk/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/Views.java}
Use the marker classes with the @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-jackson/src/test/java/org/apache/camel/component/jackson/TestPojoView.java}
Finally use the Camel JacksonDataFormat to marshall the above POJO to JSON.
Wiki Markup
{snippet:id=format|lang=java|url=camel/trunk/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/JacksonMarshalViewTest.java}
Note that the weight height field is missing in the resulting JSON:

Code Block
{"age":30, "heightweight":19070}

The GSON library supports a similar feature through the notion of ExclusionStrategies:

Wiki Markup
{snippet:id=strategy|lang=java|url=camel/trunk/components/camel-gson/src/test/java/org/apache/camel/component/gson/GsonMarshalExclusionTest.java}
The GsonDataFormat accepts an ExclusionStrategy in its constructor:
Wiki Markup
{snippet:id=format|lang=java|url=camel/trunk/components/camel-gson/src/test/java/org/apache/camel/component/gson/GsonMarshalExclusionTest.java}
The line above will exclude fields annotated with @ExcludeAge when marshalling to JSON.

...