JSONJSON is a Data Format to marshal and unmarshal Java objects to and from JSON. For JSON to object marshalling, Camel provides integration with three popular JSON libraries:
By default Camel uses the XStream library.
Using JSON data format with the XStream library// lets turn Object messages into json then send to MQSeries from("activemq:My.Queue"). marshal().json(). to("mqseries:Another.Queue"); Using JSON data format with the Jackson library// lets turn Object messages into json then send to MQSeries from("activemq:My.Queue"). marshal().json(JsonLibrary.Jackson). to("mqseries:Another.Queue"); Using JSON data format with the GSON library// lets turn Object messages into json then send to MQSeries from("activemq:My.Queue"). marshal().json(JsonLibrary.Gson). to("mqseries:Another.Queue"); Using JSON in Spring DSLWhen 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 Json 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 -->
<json id="jack" library="Jackson" unmarshalTypeName="org.apache.camel.component.jackson.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 marshallingAs of Camel 2.10 public class Views { static class Weight { } static class Age { } } Use the marker classes with the @JsonView annotation to include/exclude certain fields. The annotation also works on getters. @JsonView(Views.Age.class) private int age = 30; private int height = 190; @JsonView(Views.Weight.class) private int weight = 70; Finally use the Camel JacksonDataFormat to marshall the above POJO to JSON. JacksonDataFormat ageViewFormat = new JacksonDataFormat(TestPojoView.class, Views.Age.class); from("direct:inPojoAgeView").marshal(ageViewFormat); Note that the weight field is missing in the resulting JSON:
{"age":30, "height":190}
The GSON library supports a similar feature through the notion of ExclusionStrategies: /**
* Strategy to exclude {@link ExcludeAge} annotated fields
*/
protected static class AgeExclusionStrategy implements ExclusionStrategy {
@Override
public boolean shouldSkipField(FieldAttributes f) {
return f.getAnnotation(ExcludeAge.class) != null;
}
@Override
public boolean shouldSkipClass(Class<?> clazz) {
return false;
}
}
The GsonDataFormat accepts an ExclusionStrategy in its constructor: GsonDataFormat ageExclusionFormat = new GsonDataFormat(TestPojoExclusion.class, new AgeExclusionStrategy()); from("direct:inPojoExcludeAge").marshal(ageExclusionFormat); The line above will exclude fields annotated with @ExcludeAge when marshalling to JSON. Configuring field naming policyAvailable as of Camel 2.11 The GSON library supports specifying policies and strategies for mapping from json to POJO fields. A common naming convention is to map json fields using lower case with underscores. We may have this JSON string
{
"id" : 123,
"first_name" : "Donald"
"last_name" : "Duck"
}
Which we want to map to a POJO that has getter/setters as public class PersonPojo { private int id; private String firstName; private String lastName; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } } Then we can configure the org.apache.camel.component.gson.GsonDataFormat in a Spring XML files as shown below. Notice we use fieldNamingPolicy property to set the field mapping. This property is an enum from GSon com.google.gson.FieldNamingPolicy which has a number of pre defined mappings. If you need full control you can use the property FieldNamingStrategy and implement a custom com.google.gson.FieldNamingStrategy where you can control the mapping.
<!-- define the gson data format, where we configure the data format using the properties -->
<bean id="gson" class="org.apache.camel.component.gson.GsonDataFormat">
<!-- we want to unmarshal to person pojo -->
<property name="unmarshalType" value="org.apache.camel.component.gson.PersonPojo"/>
<!-- we want to map fields to use lower case and underscores -->
<property name="fieldNamingPolicy" value="LOWER_CASE_WITH_UNDERSCORES"/>
</bean>
And use it in Camel routes by referring to its bean id as shown: <camelContext xmlns="http://camel.apache.org/schema/spring"> <route> <from uri="direct:inPojo"/> <marshal ref="gson"/> </route> <route> <from uri="direct:backPojo"/> <unmarshal ref="gson"/> </route> </camelContext> Dependencies for XStreamTo use JSON in your camel routes you need to add the a dependency on camel-xstream 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-xstream</artifactId> <version>2.9.2</version> </dependency> Dependencies for JacksonTo use JSON in your camel routes you need to add the a dependency on camel-jackson 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-jackson</artifactId> <version>2.9.2</version> </dependency> Dependencies for GSONTo use JSON in your camel routes you need to add the a dependency on camel-gson 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-gson</artifactId> <version>2.10.0</version> </dependency> |