JSON Jackson
Since Camel 2.0
Jackson is a Data Format that uses the Jackson Library
from("activemq:My.Queue").
marshal().json(JsonLibrary.Jackson).
to("mqseries:Another.Queue");
Jackson Options
The JSON Jackson dataformat supports 21 options, which are listed below.
Name | Default | Java Type | Description |
---|---|---|---|
| Lookup and use the existing ObjectMapper with the given id when using Jackson. | ||
|
| Whether to lookup and use default Jackson ObjectMapper from the registry. | |
|
| If set to true then Jackson will look for an objectMapper to use from the registry. | |
|
| To enable pretty printing output nicely formatted. Is by default false. | |
|
| Force using generator that outputs JSON content using a java.io.Writer which handles character encoding. This should be preferred when using 2-byte/4-byte characters such as Japanese. | |
| Class name of the java type to use when unmarshalling. | ||
| 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. This option is to refer to the class which has JsonView annotations. | ||
| 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, you can set this option to NON_NULL. | ||
|
| Used for JMS users to allow the JMSType header from the JMS spec to specify a FQN classname to use to unmarshal to. | |
| Refers to a custom collection type to lookup in the registry to use. This option should rarely be used, but allows using different collection types than java.util.Collection based as default. | ||
|
| To unmarshal to a List of Map or a List of Pojo. | |
| To use custom Jackson modules com.fasterxml.jackson.databind.Module specified as a String with FQN class names. Multiple classes can be separated by comma. | ||
| To use custom Jackson modules referred from the Camel registry. Multiple modules can be separated by comma. | ||
| Set of features to enable on the Jackson com.fasterxml.jackson.databind.ObjectMapper. The features should be a name that matches a enum from com.fasterxml.jackson.databind.SerializationFeature, com.fasterxml.jackson.databind.DeserializationFeature, or com.fasterxml.jackson.databind.MapperFeature Multiple features can be separated by comma. | ||
| Set of features to disable on the Jackson com.fasterxml.jackson.databind.ObjectMapper. The features should be a name that matches a enum from com.fasterxml.jackson.databind.SerializationFeature, com.fasterxml.jackson.databind.DeserializationFeature, or com.fasterxml.jackson.databind.MapperFeature Multiple features can be separated by comma. | ||
|
| If enabled then Jackson is allowed to attempt to use the CamelJacksonUnmarshalType header during the unmarshalling. This should only be enabled when desired to be used. | |
| If set then Jackson will use the Timezone when marshalling/unmarshalling. This option will have no effect on the others Json DataFormat, like gson and fastjson. | ||
| Optional schema resolver used to lookup schemas for the data in transit. | ||
|
| When not disabled, the SchemaResolver will be looked up into the registry. | |
| If set then Jackson will use the the defined Property Naming Strategy.Possible values are: LOWER_CAMEL_CASE, LOWER_DOT_CASE, LOWER_CASE, KEBAB_CASE, SNAKE_CASE and UPPER_CAMEL_CASE. | ||
|
| Whether the data format should set the Content-Type header with the type from the data format. For example application/xml for data formats marshalling to XML, or application/json for data formats marshalling to JSON. |
Usage
2 and 4 bytes characters
Jackson will default work with UTF-8 using an optimized JSon generator that only supports UTF-8. For users that need 2-bytes or 4-bytes (such as Japanese) would need to turn on useWriter=true
in the Camel dataformat, to use another JSon generator that lets java.io.Writer
handle character encodings.
Using custom ObjectMapper
You can configure JacksonDataFormat
to use a custom ObjectMapper
in case you need more control of the mapping configuration.
If you set up a single ObjectMapper
in the registry, then Camel will automatic lookup and use this ObjectMapper
. For example, if you use Spring Boot, then Spring Boot can provide a default ObjectMapper
for you if you have Spring MVC enabled. And this would allow Camel to detect that there is one bean of ObjectMapper
class type in the Spring Boot bean registry and then use it. When this happens you should set a INFO
logging from Camel.
Using Jackson for automatic type conversion
The camel-jackson
module allows integrating Jackson as a Type Converter.
This gives a set of out-of-the-box converters to/from the Jackson type JSonNode
, such as converting from JSonNode
to String
or vice versa.
Enabling more type converters and support for POJOs
To enable POJO conversion support for camel-jackson
then this must be enabled, which is done by setting the following options on the CamelContext
global options, as shown:
// Enable Jackson JSON type converter for more types.
camelContext.getGlobalOptions().put("CamelJacksonEnableTypeConverter", "true");
// Allow Jackson JSON to convert to pojo types also
// (by default, Jackson only converts to String and other simple types)
getContext().getGlobalOptions().put("CamelJacksonTypeConverterToPojo", "true");
The camel-jackson
type converter integrates with JAXB which means you can annotate POJO class with JAXB
annotations that Jackson can use. You can also use Jackson’s own annotations in your POJO classes.
Dependencies
To use Jackson in your Camel routes, you need to add the dependency on camel-jackson, which implements this data format.
If you use Maven, you could add the following to your pom.xml
, substituting the version number for the latest & greatest release:
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jackson</artifactId>
<version>x.x.x</version>
<!-- use the same version as your Camel core version -->
</dependency>
Spring Boot Auto-Configuration
When using jackson with Spring Boot make sure to use the following Maven dependency to have support for auto configuration:
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-jackson-starter</artifactId>
<version>x.x.x</version>
<!-- use the same version as your Camel core version -->
</dependency>
The component supports 22 options, which are listed below.
Name | Description | Default | Type |
---|---|---|---|
Used for JMS users to allow the JMSType header from the JMS spec to specify a FQN classname to use to unmarshal to. | false | Boolean | |
If enabled then Jackson is allowed to attempt to use the CamelJacksonUnmarshalType header during the unmarshalling. This should only be enabled when desired to be used. | false | Boolean | |
If set to true then Jackson will look for an objectMapper to use from the registry. | false | Boolean | |
When not disabled, the SchemaResolver will be looked up into the registry. | true | Boolean | |
Refers to a custom collection type to lookup in the registry to use. This option should rarely be used, but allows using different collection types than java.util.Collection based as default. | String | ||
Whether the data format should set the Content-Type header with the type from the data format. For example application/xml for data formats marshalling to XML, or application/json for data formats marshalling to JSON. | true | Boolean | |
Set of features to disable on the Jackson com.fasterxml.jackson.databind.ObjectMapper. The features should be a name that matches a enum from com.fasterxml.jackson.databind.SerializationFeature, com.fasterxml.jackson.databind.DeserializationFeature, or com.fasterxml.jackson.databind.MapperFeature Multiple features can be separated by comma. | String | ||
Set of features to enable on the Jackson com.fasterxml.jackson.databind.ObjectMapper. The features should be a name that matches a enum from com.fasterxml.jackson.databind.SerializationFeature, com.fasterxml.jackson.databind.DeserializationFeature, or com.fasterxml.jackson.databind.MapperFeature Multiple features can be separated by comma. | String | ||
Whether to enable auto configuration of the jackson data format. This is enabled by default. | Boolean | ||
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, you can set this option to NON_NULL. | String | ||
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. This option is to refer to the class which has JsonView annotations. | String | ||
To use custom Jackson modules com.fasterxml.jackson.databind.Module specified as a String with FQN class names. Multiple classes can be separated by comma. | String | ||
To use custom Jackson modules referred from the Camel registry. Multiple modules can be separated by comma. | String | ||
If set then Jackson will use the the defined Property Naming Strategy.Possible values are: LOWER_CAMEL_CASE, LOWER_DOT_CASE, LOWER_CASE, KEBAB_CASE, SNAKE_CASE and UPPER_CAMEL_CASE. | String | ||
Lookup and use the existing ObjectMapper with the given id when using Jackson. | String | ||
To enable pretty printing output nicely formatted. Is by default false. | false | Boolean | |
Optional schema resolver used to lookup schemas for the data in transit. The option is a org.apache.camel.component.jackson.SchemaResolver type. | String | ||
If set then Jackson will use the Timezone when marshalling/unmarshalling. This option will have no effect on the others Json DataFormat, like gson and fastjson. | String | ||
Class name of the java type to use when unmarshalling. | String | ||
Whether to lookup and use default Jackson ObjectMapper from the registry. | true | Boolean | |
To unmarshal to a List of Map or a List of Pojo. | false | Boolean | |
Force using generator that outputs JSON content using a java.io.Writer which handles character encoding. This should be preferred when using 2-byte/4-byte characters such as Japanese. | false | Boolean |