Versions Compared

Key

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

...

Using JSON data format with the XStream library

Code Block

// 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

Code Block

// 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

Code Block

// lets turn Object messages into json then send to MQSeries
from("activemq:My.Queue").
  marshal().json(JsonLibrary.Gson).
  to("mqseries:Another.Queue");

...

When using Data Format in Spring DSL you need to declare the data formats first. This is done in the DataFormats XML tag.

Code Block
xml
languagexml

        <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:

Code Block
xml
languagexml

       <route>
            <from uri="direct:back"/>
            <unmarshal ref="jack"/>
            <to uri="mock:reverse"/>
        </route>

...

Note that the weight field is missing in the resulting JSON:

Code Block

{"age":30, "height":190}

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

...

We may have this JSON string

Code Block

{
  "id" : 123,
  "first_name" : "Donald"
  "last_name" : "Duck"
}

Which we want to map to a POJO that has getter/setters as

Code Block
languagejava
java:titlePersonPojo.java
titlePersonPojo.java

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;
    }
}

...

Code Block
xml:titleConfiguring GsonDataFormat in Spring XML file

    <!-- 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>

...

Code Block
xml:titleUsing gson from Camel routes

   <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
java
java

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
java
java

from("direct:inPojoAgeView").
  marshal().json(TestPojoView.class, Views.Age.class);

And the same in XML DSL:

Code Block
java
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
xml
xml

<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
xml
xml

<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
xml
xml

<dependency>
  <groupId>org.apache.camel</groupId>
  <artifactId>camel-gson</artifactId>
  <version>2.10.0</version>
</dependency>