...
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.
Code Block |
title |
Configuring GsonDataFromat in Spring XML file |
xml:title |
Configuring GsonDataFormat in Spring XML file |
language |
xml |
|
<!-- 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:
Code Block |
title |
Using gson from Camel Routes |
xml:title |
Using gson from Camel routes |
language |
xml |
|
<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>
|
...
As an example of using this attribute you can instead of:
Code Block |
|
JacksonDataFormat ageViewFormat = new JacksonDataFormat(TestPojoView.class, Views.Age.class);
from("direct:inPojoAgeView").
marshal(ageViewFormat);
|
Directly specify your JSON view inside the Java DSL as:
Code Block |
|
from("direct:inPojoAgeView").
marshal().json(TestPojoView.class, Views.Age.class);
|
And the same in XML DSL:
Code Block |
java
|
<from uri="direct:inPojoAgeView"/>
<marshal>
<json library="Jackson" unmarshalTypeName="org.apache.camel.component.jackson.TestPojoView" jsonView="org.apache.camel.component.jackson.Views$Age"/>
</marshal>
|
...
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).
Code Block |
|
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-xstream</artifactId>
<version>2.9.2</version>
</dependency>
|
...
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).
Code Block |
|
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jackson</artifactId>
<version>2.9.2</version>
</dependency>
|
...
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).
Code Block |
|
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-gson</artifactId>
<version>2.10.0</version>
</dependency>
|