JSON

Apache Camel: JSON

字号+ 作者:H5之家 来源:H5之家 2017-05-01 18:01 我要评论( )

JSON JSON 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: Every library requires adding the special camel component (see

JSON

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

Every library requires adding the special camel component (see "Dependency..." paragraphs further down). By default Camel uses the XStream library.

Direct, bi-directional JSON <=> XML conversions

As of Camel 2.10, Camel supports direct, bi-directional JSON <=> XML conversions via the camel-xmljson data format, which is documented separately.

Using JSON Data Format With the XStream LibraryUsing JSON Data Format With the Jackson LibraryUsing JSON Data Format With the GSON LibraryUsing JSON in Spring DSL

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

And then you can refer to this id in the route:

Excluding POJO Fields From Marshalling

As of Camel 2.10
When marshaling 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:

Second, use the marker classes with the @JsonView annotation to include/exclude certain fields. The annotation also works on getters:Finally, use the Camel JacksonDataFormat to marshal the above POJO to JSON.Note: the height field is missing in the resulting JSON.

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

The GsonDataFormat accepts an ExclusionStrategy in its constructor:The line above will exclude fields annotated with @ExcludeAge when marshaling to JSON.Configuring Field Naming Policy

Available 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

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

PersonPojo.java

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 predefined 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.

Configuring GsonDataFromat in Spring XML file

And use it in Camel routes by referring to its bean id as shown:

Using gson from Camel Routes

Include/Exclude Fields Using the jsonView Attribute With JacksonDataFormat

Available as of Camel 2.12

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

Directly specify your JSON view inside the Java DSL as:

And the same in XML DSL:

Setting Serialization Include Option for Jackson Marshal

Available as of Camel 2.13.3/2.14

If you want to marshal a POJO to JSON, 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, 

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

Or from XML DSL you configure this as

Unmarshaling from JSON to POJO with Dynamic Class Name

Available as of Camel 2.14

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 JSON payload as. Notice that behavior is enabled out of the box from Camel 2.14. 

 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:

Or from XML DSL you configure this as

Unmarshaling From JSON to List<Map> or List<pojo>

Available as of Camel 2.14

If you are using Jackson to unmarshal JSON to a list of map/POJO, you can now specify this by setting useList="true" or use the org.apache.camel.component.jackson.ListJacksonDataFormat.

For example, with Java you can do as shown below:

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

And you can specify the pojo type also

Using Custom Jackson's ObjectMapper

Available from Camel 2.17

You can use custom Jackson ObjectMapper instance, can be configured as shown below.

Where myMapper is the id of the custom instance that Camel will lookup in the Registry.

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.

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • RESTful API 设计最佳实践

    RESTful API 设计最佳实践

    2017-05-02 10:03

  • Play 2.0: Akka, Rest, Json and dependencies

    Play 2.0: Akka, Rest, Json and dependencies

    2017-05-01 10:01

  • Jquery+Json实现无刷新分页插件

    Jquery+Json实现无刷新分页插件

    2017-05-01 09:04

  • js获取json元素数量的方法教程

    js获取json元素数量的方法教程

    2017-05-01 08:02

网友点评