This is an automated email from the ASF dual-hosted git repository. nferraro pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel.git
commit 36354def3de020b480d16df875afcc5b1010d06d Author: nicolaferraro <ni.ferr...@gmail.com> AuthorDate: Fri May 14 14:31:30 2021 +0200 Update doc for avro and protobuf jackson dataformats --- .../catalog/docs/avro-jackson-dataformat.adoc | 331 ++------------------- .../catalog/docs/protobuf-jackson-dataformat.adoc | 329 ++------------------ .../src/main/docs/avro-jackson-dataformat.adoc | 331 ++------------------- .../src/main/docs/protobuf-jackson-dataformat.adoc | 329 ++------------------ docs/components/modules/dataformats/nav.adoc | 4 +- .../dataformats/pages/avro-jackson-dataformat.adoc | 331 ++------------------- .../pages/protobuf-jackson-dataformat.adoc | 329 ++------------------ 7 files changed, 149 insertions(+), 1835 deletions(-) diff --git a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/avro-jackson-dataformat.adoc b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/avro-jackson-dataformat.adoc index adfec7c..ec9f568 100644 --- a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/avro-jackson-dataformat.adoc +++ b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/avro-jackson-dataformat.adoc @@ -1,45 +1,41 @@ [[avro-jackson-dataformat]] -= Avro DataFormat -:docTitle: Avro += Avro Jackson DataFormat +:docTitle: Avro Jackson :artifactId: camel-jackson-avro :description: Marshal POJOs to Avro and back using Jackson -:since: 2.14 +:since: 3.10 :supportLevel: Stable -include::{cq-version}@camel-quarkus:ROOT:partial$reference/dataformats/avro-jackson.adoc[opts=optional] *Since Camel {since}* -Jackson XML is a Data Format which uses the -http://wiki.fasterxml.com/JacksonHome/[Jackson library] with the -https://github.com/FasterXML/jackson-dataformat-xml[XMLMapper extension] -to unmarshal an XML payload into Java objects or to marshal Java objects -into an XML payload. +Jackson Avro is a Data Format which uses the +http://wiki.fasterxml.com/JacksonHome/[Jackson library] with the https://github.com/FasterXML/jackson-dataformats-binary[Avro extension] +to unmarshal an Avro payload into Java objects or to marshal Java objects +into an Avro payload. [TIP] ==== -If you are familiar with Jackson, this XML data format behaves in the +If you are familiar with Jackson, this Avro data format behaves in the same way as its JSON counterpart, and thus can be used with classes annotated for JSON serialization/deserialization. ==== -This extension also mimics -https://github.com/FasterXML/jackson-dataformat-xml/blob/master/README.md[JAXB's -"Code first" approach]. - -This data format relies on -http://wiki.fasterxml.com/WoodstoxHome[Woodstox] (especially for -features like pretty printing), a fast and efficient XML processor. - [source,java] ------------------------------- -from("activemq:My.Queue"). - unmarshal().jacksonxml(). - to("mqseries:Another.Queue"); +from("kafka:topic"). + unmarshal().avro(AvroLibrary.Jackson, JsonNode.class). + to("log:info"); ------------------------------- -== JacksonXML Options +== Configuring the SchemaResolver + +Since Avro serialization is schema-based, this data format requires that you provide a SchemaResolver object +that is able to lookup the schema for each exchange that is going to be marshalled/unmarshalled. +You can add a single SchemaResolver to the registry and it will be looked up automatically. +Or you can explicitly specify the reference to a custom SchemaResolver. +== Avro Jackson Options // dataformat options: START The Avro dataformat supports 18 options, which are listed below. @@ -49,6 +45,7 @@ The Avro dataformat supports 18 options, which are listed below. [width="100%",cols="2s,1m,1m,6",options="header"] |=== | Name | Default | Java Type | Description +| contentTypeHeader | true | Boolean | 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 | objectMapper | | String | Lookup and use the existing ObjectMapper with the given id when using Jackson. | useDefaultObjectMapper | true | Boolean | Whether to lookup and use default Jackson ObjectMapper from the registry. | unmarshalTypeName | | String | Class name of the java type to use when unmarshalling @@ -64,298 +61,22 @@ The Avro dataformat supports 18 options, which are listed below. | allowUnmarshallType | 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. | timezone | | String | If set then Jackson will use the Timezone when marshalling/unmarshalling. | autoDiscoverObjectMapper | false | Boolean | If set to true then Jackson will lookup for an objectMapper into the registry -| contentTypeHeader | true | Boolean | 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 | schemaResolver | | String | Optional schema resolver used to lookup schemas for the data in transit. | autoDiscoverSchemaResolver | true | Boolean | When not disabled, the SchemaResolver will be looked up into the registry |=== // dataformat options: END -=== Using Jackson XML 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. - -[source,xml] ------------------------------------------------------------------------------------------------------------------------------ - <dataFormats> - <!-- here we define a Xml 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 --> - <jacksonxml id="jack" unmarshalTypeName="org.apache.camel.component.jacksonxml.TestPojo"/> - </dataFormats> ------------------------------------------------------------------------------------------------------------------------------ - -And then you can refer to this id in the route: - -[source,xml] -------------------------------------- - <route> - <from uri="direct:back"/> - <unmarshal><custom ref="jack"/></unmarshal> - <to uri="mock:reverse"/> - </route> -------------------------------------- - -== Excluding POJO fields from marshalling - -When marshalling a POJO to XML you might want to exclude certain fields -from the XML output. With Jackson you can -use http://wiki.fasterxml.com/JacksonJsonViews[JSON views] to accomplish -this. First create one or more marker classes. - -Use the marker classes with the `@JsonView` annotation to -include/exclude certain fields. The annotation also works on getters. - -Finally use the Camel `JacksonXMLDataFormat` to marshall the above POJO -to XML. - -Note that the weight field is missing in the resulting XML: - -[source,java] ----------------------------- -<pojo age="30" weight="70"/> ----------------------------- - -== Include/Exclude fields using the `jsonView` attribute with `JacksonXML`DataFormat - -As an example of using this attribute you can instead of: - -[source,java] ---------------------------------------------------------------------------------------------------- -JacksonXMLDataFormat ageViewFormat = new JacksonXMLDataFormat(TestPojoView.class, Views.Age.class); -from("direct:inPojoAgeView"). - marshal(ageViewFormat); ---------------------------------------------------------------------------------------------------- - -Directly specify your http://wiki.fasterxml.com/JacksonJsonViews[JSON -view] inside the Java DSL as: - -[source,java] ------------------------------------------------------------- -from("direct:inPojoAgeView"). - marshal().jacksonxml(TestPojoView.class, Views.Age.class); ------------------------------------------------------------- - -And the same in XML DSL: - -[source,xml] ---------------------------------------------------------------------------------------------------------------------------------------------------- -<from uri="direct:inPojoAgeView"/> - <marshal> - <jacksonxml unmarshalTypeName="org.apache.camel.component.jacksonxml.TestPojoView" jsonView="org.apache.camel.component.jacksonxml.Views$Age"/> - </marshal> ---------------------------------------------------------------------------------------------------------------------------------------------------- - -== Setting serialization include option - -If you want to marshal a pojo to XML, 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, - -[source,java] ------------------------------- -@JsonInclude(Include.NON_NULL) -public class MyPojo { - ... -} ------------------------------- - -But this requires you to include that annotation in your pojo source -code. You can also configure the Camel JacksonXMLDataFormat to set the -include option, as shown below: - -[source,java] ---------------------------------------------------------- -JacksonXMLDataFormat format = new JacksonXMLDataFormat(); -format.setInclude("NON_NULL"); ---------------------------------------------------------- - -Or from XML DSL you configure this as - -[source,java] ------------------------------------------------------- - <dataFormats> - <jacksonxml id="jacksonxml" include="NON_NULL"/> - </dataFormats> ------------------------------------------------------- - -== Unmarshalling from XML to POJO with dynamic class name - -If you use jackson to unmarshal XML 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 XML payload as. - - 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: - -[source,java] ---------------------------------------------------- -JacksonDataFormat format = new JacksonDataFormat(); -format.setAllowJmsType(true); ---------------------------------------------------- - -Or from XML DSL you configure this as - -[source,java] -------------------------------------------------------- - <dataFormats> - <jacksonxml id="jacksonxml" allowJmsType="true"/> - </dataFormats> -------------------------------------------------------- - -== Unmarshalling from XML to List<Map> or List<pojo> - -If you are using Jackson to unmarshal XML to a list of map/pojo, you can -now specify this by setting `useList="true"` or use -the `org.apache.camel.component.jacksonxml.ListJacksonXMLDataFormat`. -For example with Java you can do as shown below: - -[source,java] -------------------------------------------------------------- -JacksonXMLDataFormat format = new ListJacksonXMLDataFormat(); -// or -JacksonXMLDataFormat format = new JacksonXMLDataFormat(); -format.useList(); -// and you can specify the pojo class type also -format.setUnmarshalType(MyPojo.class); -------------------------------------------------------------- - -And if you use XML DSL then you configure to use list -using `useList` attribute as shown below: - -[source,java] --------------------------------------------- - <dataFormats> - <jacksonxml id="jack" useList="true"/> - </dataFormats> --------------------------------------------- - -And you can specify the pojo type also +== Using custom AvroMapper -[source,java] -------------------------------------------------------------------------------- - <dataFormats> - <jacksonxml id="jack" useList="true" unmarshalTypeName="com.foo.MyPojo"/> - </dataFormats> -------------------------------------------------------------------------------- - -== Using custom Jackson modules - -You can use custom Jackson modules by specifying the class names of -those using the moduleClassNames option as shown below. +You can configure `JacksonAvroDataFormat` to use a custom `AvroMapper` in case you need more control of the mapping configuration. -[source,java] ------------------------------------------------------------------------------------------------------------------------------------------ - <dataFormats> - <jacksonxml id="jack" useList="true" unmarshalTypeName="com.foo.MyPojo" moduleClassNames="com.foo.MyModule,com.foo.MyOtherModule"/> - </dataFormats> ------------------------------------------------------------------------------------------------------------------------------------------ - -When using moduleClassNames then the custom jackson modules are not -configured, by created using default constructor and used as-is. If a -custom module needs any custom configuration, then an instance of the -module can be created and configured, and then use modulesRefs to refer -to the module as shown below: - -[source,java] ------------------------------------------------------------------------------------------------------------------- - <bean id="myJacksonModule" class="com.foo.MyModule"> - ... // configure the module as you want - </bean> - - <dataFormats> - <jacksonxml id="jacksonxml" useList="true" unmarshalTypeName="com.foo.MyPojo" moduleRefs="myJacksonModule"/> - </dataFormats> ------------------------------------------------------------------------------------------------------------------- - - Multiple modules can be specified separated by comma, such as -moduleRefs="myJacksonModule,myOtherModule" - -== Enabling or disable features using Jackson - -Jackson has a number of features you can enable or disable, which its -ObjectMapper uses. For example to disable failing on unknown properties -when marshalling, you can configure this using the disableFeatures: - -[source,java] -------------------------------------------------------------------------------------------------------------------- - <dataFormats> - <jacksonxml id="jacksonxml" unmarshalTypeName="com.foo.MyPojo" disableFeatures="FAIL_ON_UNKNOWN_PROPERTIES"/> - </dataFormats> -------------------------------------------------------------------------------------------------------------------- - -You can disable multiple features by separating the values using comma. -The values for the features must be the name of the enums from Jackson -from the following enum classes - -* com.fasterxml.jackson.databind.SerializationFeature -* com.fasterxml.jackson.databind.DeserializationFeature -* com.fasterxml.jackson.databind.MapperFeature - -To enable a feature use the enableFeatures options instead. - -From Java code you can use the type safe methods from camel-jackson -module: - -[source,java] ----------------------------------------------------------------------- -JacksonDataFormat df = new JacksonDataFormat(MyPojo.class); -df.disableFeature(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); -df.disableFeature(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES); ----------------------------------------------------------------------- - -== Converting Maps to POJO using Jackson - -Jackson `ObjectMapper` can be used to convert maps to POJO objects. -Jackson component comes with the data converter that can be used to -convert `java.util.Map` instance to non-String, non-primitive and -non-Number objects. - -[source,java] ----------------------------------------------------------------- -Map<String, Object> invoiceData = new HashMap<String, Object>(); -invoiceData.put("netValue", 500); -producerTemplate.sendBody("direct:mapToInvoice", invoiceData); -... -// Later in the processor -Invoice invoice = exchange.getIn().getBody(Invoice.class); ----------------------------------------------------------------- - -If there is a single `ObjectMapper` instance available in the Camel -registry, it will used by the converter to perform the conversion. -Otherwise the default mapper will be used. - -== Formatted XML marshalling (pretty-printing) - -Using the `prettyPrint` option one can output a well formatted XML while -marshalling: - -[source,java] ------------------------------------------------- - <dataFormats> - <jacksonxml id="jack" prettyPrint="true"/> - </dataFormats> ------------------------------------------------- - -And in Java DSL: - -[source,java] ---------------------------------------------------- -from("direct:inPretty").marshal().jacksonxml(true); ---------------------------------------------------- - -Please note that there are 5 different overloaded `jacksonxml()` DSL -methods which support the `prettyPrint` option in combination with other -settings for `unmarshalType`, `jsonView` etc. +If you setup a single `AvroMapper` in the registry, then Camel will automatic lookup and use this `AvroMapper`. == Dependencies -To use Jackson XML in your camel routes you need to add the dependency -on *camel-jacksonxml* which implements this data format. +To use Avro Jackson in your camel routes you need to add the dependency +on *camel-jackson-avro* 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 @@ -365,10 +86,8 @@ the download page for the latest versions). ---------------------------------------------------------- <dependency> <groupId>org.apache.camel</groupId> - <artifactId>camel-jacksonxml</artifactId> + <artifactId>camel-jackson-avro</artifactId> <version>x.x.x</version> <!-- use the same version as your Camel core version --> </dependency> ---------------------------------------------------------- - -include::{page-component-version}@camel-spring-boot::page$jacksonxml-starter.adoc[] diff --git a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/protobuf-jackson-dataformat.adoc b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/protobuf-jackson-dataformat.adoc index d486c52..7f6136f 100644 --- a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/protobuf-jackson-dataformat.adoc +++ b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/protobuf-jackson-dataformat.adoc @@ -1,45 +1,41 @@ [[protobuf-jackson-dataformat]] -= Protobuf DataFormat -:docTitle: Protobuf += Protobuf Jackson DataFormat +:docTitle: Protobuf Jackson :artifactId: camel-jackson-protobuf :description: Marshal POJOs to Protobuf and back using Jackson -:since: 2.2 +:since: 3.10 :supportLevel: Stable -include::{cq-version}@camel-quarkus:ROOT:partial$reference/dataformats/protobuf-jackson.adoc[opts=optional] *Since Camel {since}* -Jackson XML is a Data Format which uses the -http://wiki.fasterxml.com/JacksonHome/[Jackson library] with the -https://github.com/FasterXML/jackson-dataformat-xml[XMLMapper extension] -to unmarshal an XML payload into Java objects or to marshal Java objects -into an XML payload. +Jackson Protobuf is a Data Format which uses the +http://wiki.fasterxml.com/JacksonHome/[Jackson library] with the https://github.com/FasterXML/jackson-dataformats-binary[Protobuf extension] +to unmarshal a Protobuf payload into Java objects or to marshal Java objects +into a Protobuf payload. [TIP] ==== -If you are familiar with Jackson, this XML data format behaves in the +If you are familiar with Jackson, this Protobuf data format behaves in the same way as its JSON counterpart, and thus can be used with classes annotated for JSON serialization/deserialization. ==== -This extension also mimics -https://github.com/FasterXML/jackson-dataformat-xml/blob/master/README.md[JAXB's -"Code first" approach]. - -This data format relies on -http://wiki.fasterxml.com/WoodstoxHome[Woodstox] (especially for -features like pretty printing), a fast and efficient XML processor. - [source,java] ------------------------------- -from("activemq:My.Queue"). - unmarshal().jacksonxml(). - to("mqseries:Another.Queue"); +from("kafka:topic"). + unmarshal().protobuf(ProtobufLibrary.Jackson, JsonNode.class). + to("log:info"); ------------------------------- -== JacksonXML Options +== Configuring the SchemaResolver + +Since Protobuf serialization is schema-based, this data format requires that you provide a SchemaResolver object +that is able to lookup the schema for each exchange that is going to be marshalled/unmarshalled. +You can add a single SchemaResolver to the registry and it will be looked up automatically. +Or you can explicitly specify the reference to a custom SchemaResolver. +== Protobuf Jackson Options // dataformat options: START The Protobuf dataformat supports 18 options, which are listed below. @@ -71,291 +67,16 @@ The Protobuf dataformat supports 18 options, which are listed below. // dataformat options: END -=== Using Jackson XML 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. - -[source,xml] ------------------------------------------------------------------------------------------------------------------------------ - <dataFormats> - <!-- here we define a Xml 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 --> - <jacksonxml id="jack" unmarshalTypeName="org.apache.camel.component.jacksonxml.TestPojo"/> - </dataFormats> ------------------------------------------------------------------------------------------------------------------------------ - -And then you can refer to this id in the route: - -[source,xml] -------------------------------------- - <route> - <from uri="direct:back"/> - <unmarshal><custom ref="jack"/></unmarshal> - <to uri="mock:reverse"/> - </route> -------------------------------------- - -== Excluding POJO fields from marshalling - -When marshalling a POJO to XML you might want to exclude certain fields -from the XML output. With Jackson you can -use http://wiki.fasterxml.com/JacksonJsonViews[JSON views] to accomplish -this. First create one or more marker classes. - -Use the marker classes with the `@JsonView` annotation to -include/exclude certain fields. The annotation also works on getters. - -Finally use the Camel `JacksonXMLDataFormat` to marshall the above POJO -to XML. - -Note that the weight field is missing in the resulting XML: - -[source,java] ----------------------------- -<pojo age="30" weight="70"/> ----------------------------- - -== Include/Exclude fields using the `jsonView` attribute with `JacksonXML`DataFormat - -As an example of using this attribute you can instead of: - -[source,java] ---------------------------------------------------------------------------------------------------- -JacksonXMLDataFormat ageViewFormat = new JacksonXMLDataFormat(TestPojoView.class, Views.Age.class); -from("direct:inPojoAgeView"). - marshal(ageViewFormat); ---------------------------------------------------------------------------------------------------- - -Directly specify your http://wiki.fasterxml.com/JacksonJsonViews[JSON -view] inside the Java DSL as: - -[source,java] ------------------------------------------------------------- -from("direct:inPojoAgeView"). - marshal().jacksonxml(TestPojoView.class, Views.Age.class); ------------------------------------------------------------- - -And the same in XML DSL: - -[source,xml] ---------------------------------------------------------------------------------------------------------------------------------------------------- -<from uri="direct:inPojoAgeView"/> - <marshal> - <jacksonxml unmarshalTypeName="org.apache.camel.component.jacksonxml.TestPojoView" jsonView="org.apache.camel.component.jacksonxml.Views$Age"/> - </marshal> ---------------------------------------------------------------------------------------------------------------------------------------------------- - -== Setting serialization include option - -If you want to marshal a pojo to XML, 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, - -[source,java] ------------------------------- -@JsonInclude(Include.NON_NULL) -public class MyPojo { - ... -} ------------------------------- - -But this requires you to include that annotation in your pojo source -code. You can also configure the Camel JacksonXMLDataFormat to set the -include option, as shown below: - -[source,java] ---------------------------------------------------------- -JacksonXMLDataFormat format = new JacksonXMLDataFormat(); -format.setInclude("NON_NULL"); ---------------------------------------------------------- - -Or from XML DSL you configure this as - -[source,java] ------------------------------------------------------- - <dataFormats> - <jacksonxml id="jacksonxml" include="NON_NULL"/> - </dataFormats> ------------------------------------------------------- - -== Unmarshalling from XML to POJO with dynamic class name - -If you use jackson to unmarshal XML 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 XML payload as. - - 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: - -[source,java] ---------------------------------------------------- -JacksonDataFormat format = new JacksonDataFormat(); -format.setAllowJmsType(true); ---------------------------------------------------- - -Or from XML DSL you configure this as - -[source,java] -------------------------------------------------------- - <dataFormats> - <jacksonxml id="jacksonxml" allowJmsType="true"/> - </dataFormats> -------------------------------------------------------- - -== Unmarshalling from XML to List<Map> or List<pojo> - -If you are using Jackson to unmarshal XML to a list of map/pojo, you can -now specify this by setting `useList="true"` or use -the `org.apache.camel.component.jacksonxml.ListJacksonXMLDataFormat`. -For example with Java you can do as shown below: - -[source,java] -------------------------------------------------------------- -JacksonXMLDataFormat format = new ListJacksonXMLDataFormat(); -// or -JacksonXMLDataFormat format = new JacksonXMLDataFormat(); -format.useList(); -// and you can specify the pojo class type also -format.setUnmarshalType(MyPojo.class); -------------------------------------------------------------- - -And if you use XML DSL then you configure to use list -using `useList` attribute as shown below: - -[source,java] --------------------------------------------- - <dataFormats> - <jacksonxml id="jack" useList="true"/> - </dataFormats> --------------------------------------------- - -And you can specify the pojo type also - -[source,java] -------------------------------------------------------------------------------- - <dataFormats> - <jacksonxml id="jack" useList="true" unmarshalTypeName="com.foo.MyPojo"/> - </dataFormats> -------------------------------------------------------------------------------- - -== Using custom Jackson modules - -You can use custom Jackson modules by specifying the class names of -those using the moduleClassNames option as shown below. - -[source,java] ------------------------------------------------------------------------------------------------------------------------------------------ - <dataFormats> - <jacksonxml id="jack" useList="true" unmarshalTypeName="com.foo.MyPojo" moduleClassNames="com.foo.MyModule,com.foo.MyOtherModule"/> - </dataFormats> ------------------------------------------------------------------------------------------------------------------------------------------ - -When using moduleClassNames then the custom jackson modules are not -configured, by created using default constructor and used as-is. If a -custom module needs any custom configuration, then an instance of the -module can be created and configured, and then use modulesRefs to refer -to the module as shown below: - -[source,java] ------------------------------------------------------------------------------------------------------------------- - <bean id="myJacksonModule" class="com.foo.MyModule"> - ... // configure the module as you want - </bean> - - <dataFormats> - <jacksonxml id="jacksonxml" useList="true" unmarshalTypeName="com.foo.MyPojo" moduleRefs="myJacksonModule"/> - </dataFormats> ------------------------------------------------------------------------------------------------------------------- - - Multiple modules can be specified separated by comma, such as -moduleRefs="myJacksonModule,myOtherModule" - -== Enabling or disable features using Jackson +== Using custom ProtobufMapper -Jackson has a number of features you can enable or disable, which its -ObjectMapper uses. For example to disable failing on unknown properties -when marshalling, you can configure this using the disableFeatures: +You can configure `JacksonProtobufDataFormat` to use a custom `ProtobufMapper` in case you need more control of the mapping configuration. -[source,java] -------------------------------------------------------------------------------------------------------------------- - <dataFormats> - <jacksonxml id="jacksonxml" unmarshalTypeName="com.foo.MyPojo" disableFeatures="FAIL_ON_UNKNOWN_PROPERTIES"/> - </dataFormats> -------------------------------------------------------------------------------------------------------------------- - -You can disable multiple features by separating the values using comma. -The values for the features must be the name of the enums from Jackson -from the following enum classes - -* com.fasterxml.jackson.databind.SerializationFeature -* com.fasterxml.jackson.databind.DeserializationFeature -* com.fasterxml.jackson.databind.MapperFeature - -To enable a feature use the enableFeatures options instead. - -From Java code you can use the type safe methods from camel-jackson -module: - -[source,java] ----------------------------------------------------------------------- -JacksonDataFormat df = new JacksonDataFormat(MyPojo.class); -df.disableFeature(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); -df.disableFeature(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES); ----------------------------------------------------------------------- - -== Converting Maps to POJO using Jackson - -Jackson `ObjectMapper` can be used to convert maps to POJO objects. -Jackson component comes with the data converter that can be used to -convert `java.util.Map` instance to non-String, non-primitive and -non-Number objects. - -[source,java] ----------------------------------------------------------------- -Map<String, Object> invoiceData = new HashMap<String, Object>(); -invoiceData.put("netValue", 500); -producerTemplate.sendBody("direct:mapToInvoice", invoiceData); -... -// Later in the processor -Invoice invoice = exchange.getIn().getBody(Invoice.class); ----------------------------------------------------------------- - -If there is a single `ObjectMapper` instance available in the Camel -registry, it will used by the converter to perform the conversion. -Otherwise the default mapper will be used. - -== Formatted XML marshalling (pretty-printing) - -Using the `prettyPrint` option one can output a well formatted XML while -marshalling: - -[source,java] ------------------------------------------------- - <dataFormats> - <jacksonxml id="jack" prettyPrint="true"/> - </dataFormats> ------------------------------------------------- - -And in Java DSL: - -[source,java] ---------------------------------------------------- -from("direct:inPretty").marshal().jacksonxml(true); ---------------------------------------------------- - -Please note that there are 5 different overloaded `jacksonxml()` DSL -methods which support the `prettyPrint` option in combination with other -settings for `unmarshalType`, `jsonView` etc. +If you setup a single `ProtobufMapper` in the registry, then Camel will automatic lookup and use this `ProtobufMapper`. == Dependencies -To use Jackson XML in your camel routes you need to add the dependency -on *camel-jacksonxml* which implements this data format. +To use Protobuf Jackson in your camel routes you need to add the dependency +on *camel-jackson-protobuf* 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 @@ -365,10 +86,8 @@ the download page for the latest versions). ---------------------------------------------------------- <dependency> <groupId>org.apache.camel</groupId> - <artifactId>camel-jacksonxml</artifactId> + <artifactId>camel-jackson-protobuf</artifactId> <version>x.x.x</version> <!-- use the same version as your Camel core version --> </dependency> ---------------------------------------------------------- - -include::{page-component-version}@camel-spring-boot::page$jacksonxml-starter.adoc[] diff --git a/components/camel-jackson-avro/src/main/docs/avro-jackson-dataformat.adoc b/components/camel-jackson-avro/src/main/docs/avro-jackson-dataformat.adoc index adfec7c..ec9f568 100644 --- a/components/camel-jackson-avro/src/main/docs/avro-jackson-dataformat.adoc +++ b/components/camel-jackson-avro/src/main/docs/avro-jackson-dataformat.adoc @@ -1,45 +1,41 @@ [[avro-jackson-dataformat]] -= Avro DataFormat -:docTitle: Avro += Avro Jackson DataFormat +:docTitle: Avro Jackson :artifactId: camel-jackson-avro :description: Marshal POJOs to Avro and back using Jackson -:since: 2.14 +:since: 3.10 :supportLevel: Stable -include::{cq-version}@camel-quarkus:ROOT:partial$reference/dataformats/avro-jackson.adoc[opts=optional] *Since Camel {since}* -Jackson XML is a Data Format which uses the -http://wiki.fasterxml.com/JacksonHome/[Jackson library] with the -https://github.com/FasterXML/jackson-dataformat-xml[XMLMapper extension] -to unmarshal an XML payload into Java objects or to marshal Java objects -into an XML payload. +Jackson Avro is a Data Format which uses the +http://wiki.fasterxml.com/JacksonHome/[Jackson library] with the https://github.com/FasterXML/jackson-dataformats-binary[Avro extension] +to unmarshal an Avro payload into Java objects or to marshal Java objects +into an Avro payload. [TIP] ==== -If you are familiar with Jackson, this XML data format behaves in the +If you are familiar with Jackson, this Avro data format behaves in the same way as its JSON counterpart, and thus can be used with classes annotated for JSON serialization/deserialization. ==== -This extension also mimics -https://github.com/FasterXML/jackson-dataformat-xml/blob/master/README.md[JAXB's -"Code first" approach]. - -This data format relies on -http://wiki.fasterxml.com/WoodstoxHome[Woodstox] (especially for -features like pretty printing), a fast and efficient XML processor. - [source,java] ------------------------------- -from("activemq:My.Queue"). - unmarshal().jacksonxml(). - to("mqseries:Another.Queue"); +from("kafka:topic"). + unmarshal().avro(AvroLibrary.Jackson, JsonNode.class). + to("log:info"); ------------------------------- -== JacksonXML Options +== Configuring the SchemaResolver + +Since Avro serialization is schema-based, this data format requires that you provide a SchemaResolver object +that is able to lookup the schema for each exchange that is going to be marshalled/unmarshalled. +You can add a single SchemaResolver to the registry and it will be looked up automatically. +Or you can explicitly specify the reference to a custom SchemaResolver. +== Avro Jackson Options // dataformat options: START The Avro dataformat supports 18 options, which are listed below. @@ -49,6 +45,7 @@ The Avro dataformat supports 18 options, which are listed below. [width="100%",cols="2s,1m,1m,6",options="header"] |=== | Name | Default | Java Type | Description +| contentTypeHeader | true | Boolean | 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 | objectMapper | | String | Lookup and use the existing ObjectMapper with the given id when using Jackson. | useDefaultObjectMapper | true | Boolean | Whether to lookup and use default Jackson ObjectMapper from the registry. | unmarshalTypeName | | String | Class name of the java type to use when unmarshalling @@ -64,298 +61,22 @@ The Avro dataformat supports 18 options, which are listed below. | allowUnmarshallType | 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. | timezone | | String | If set then Jackson will use the Timezone when marshalling/unmarshalling. | autoDiscoverObjectMapper | false | Boolean | If set to true then Jackson will lookup for an objectMapper into the registry -| contentTypeHeader | true | Boolean | 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 | schemaResolver | | String | Optional schema resolver used to lookup schemas for the data in transit. | autoDiscoverSchemaResolver | true | Boolean | When not disabled, the SchemaResolver will be looked up into the registry |=== // dataformat options: END -=== Using Jackson XML 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. - -[source,xml] ------------------------------------------------------------------------------------------------------------------------------ - <dataFormats> - <!-- here we define a Xml 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 --> - <jacksonxml id="jack" unmarshalTypeName="org.apache.camel.component.jacksonxml.TestPojo"/> - </dataFormats> ------------------------------------------------------------------------------------------------------------------------------ - -And then you can refer to this id in the route: - -[source,xml] -------------------------------------- - <route> - <from uri="direct:back"/> - <unmarshal><custom ref="jack"/></unmarshal> - <to uri="mock:reverse"/> - </route> -------------------------------------- - -== Excluding POJO fields from marshalling - -When marshalling a POJO to XML you might want to exclude certain fields -from the XML output. With Jackson you can -use http://wiki.fasterxml.com/JacksonJsonViews[JSON views] to accomplish -this. First create one or more marker classes. - -Use the marker classes with the `@JsonView` annotation to -include/exclude certain fields. The annotation also works on getters. - -Finally use the Camel `JacksonXMLDataFormat` to marshall the above POJO -to XML. - -Note that the weight field is missing in the resulting XML: - -[source,java] ----------------------------- -<pojo age="30" weight="70"/> ----------------------------- - -== Include/Exclude fields using the `jsonView` attribute with `JacksonXML`DataFormat - -As an example of using this attribute you can instead of: - -[source,java] ---------------------------------------------------------------------------------------------------- -JacksonXMLDataFormat ageViewFormat = new JacksonXMLDataFormat(TestPojoView.class, Views.Age.class); -from("direct:inPojoAgeView"). - marshal(ageViewFormat); ---------------------------------------------------------------------------------------------------- - -Directly specify your http://wiki.fasterxml.com/JacksonJsonViews[JSON -view] inside the Java DSL as: - -[source,java] ------------------------------------------------------------- -from("direct:inPojoAgeView"). - marshal().jacksonxml(TestPojoView.class, Views.Age.class); ------------------------------------------------------------- - -And the same in XML DSL: - -[source,xml] ---------------------------------------------------------------------------------------------------------------------------------------------------- -<from uri="direct:inPojoAgeView"/> - <marshal> - <jacksonxml unmarshalTypeName="org.apache.camel.component.jacksonxml.TestPojoView" jsonView="org.apache.camel.component.jacksonxml.Views$Age"/> - </marshal> ---------------------------------------------------------------------------------------------------------------------------------------------------- - -== Setting serialization include option - -If you want to marshal a pojo to XML, 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, - -[source,java] ------------------------------- -@JsonInclude(Include.NON_NULL) -public class MyPojo { - ... -} ------------------------------- - -But this requires you to include that annotation in your pojo source -code. You can also configure the Camel JacksonXMLDataFormat to set the -include option, as shown below: - -[source,java] ---------------------------------------------------------- -JacksonXMLDataFormat format = new JacksonXMLDataFormat(); -format.setInclude("NON_NULL"); ---------------------------------------------------------- - -Or from XML DSL you configure this as - -[source,java] ------------------------------------------------------- - <dataFormats> - <jacksonxml id="jacksonxml" include="NON_NULL"/> - </dataFormats> ------------------------------------------------------- - -== Unmarshalling from XML to POJO with dynamic class name - -If you use jackson to unmarshal XML 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 XML payload as. - - 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: - -[source,java] ---------------------------------------------------- -JacksonDataFormat format = new JacksonDataFormat(); -format.setAllowJmsType(true); ---------------------------------------------------- - -Or from XML DSL you configure this as - -[source,java] -------------------------------------------------------- - <dataFormats> - <jacksonxml id="jacksonxml" allowJmsType="true"/> - </dataFormats> -------------------------------------------------------- - -== Unmarshalling from XML to List<Map> or List<pojo> - -If you are using Jackson to unmarshal XML to a list of map/pojo, you can -now specify this by setting `useList="true"` or use -the `org.apache.camel.component.jacksonxml.ListJacksonXMLDataFormat`. -For example with Java you can do as shown below: - -[source,java] -------------------------------------------------------------- -JacksonXMLDataFormat format = new ListJacksonXMLDataFormat(); -// or -JacksonXMLDataFormat format = new JacksonXMLDataFormat(); -format.useList(); -// and you can specify the pojo class type also -format.setUnmarshalType(MyPojo.class); -------------------------------------------------------------- - -And if you use XML DSL then you configure to use list -using `useList` attribute as shown below: - -[source,java] --------------------------------------------- - <dataFormats> - <jacksonxml id="jack" useList="true"/> - </dataFormats> --------------------------------------------- - -And you can specify the pojo type also +== Using custom AvroMapper -[source,java] -------------------------------------------------------------------------------- - <dataFormats> - <jacksonxml id="jack" useList="true" unmarshalTypeName="com.foo.MyPojo"/> - </dataFormats> -------------------------------------------------------------------------------- - -== Using custom Jackson modules - -You can use custom Jackson modules by specifying the class names of -those using the moduleClassNames option as shown below. +You can configure `JacksonAvroDataFormat` to use a custom `AvroMapper` in case you need more control of the mapping configuration. -[source,java] ------------------------------------------------------------------------------------------------------------------------------------------ - <dataFormats> - <jacksonxml id="jack" useList="true" unmarshalTypeName="com.foo.MyPojo" moduleClassNames="com.foo.MyModule,com.foo.MyOtherModule"/> - </dataFormats> ------------------------------------------------------------------------------------------------------------------------------------------ - -When using moduleClassNames then the custom jackson modules are not -configured, by created using default constructor and used as-is. If a -custom module needs any custom configuration, then an instance of the -module can be created and configured, and then use modulesRefs to refer -to the module as shown below: - -[source,java] ------------------------------------------------------------------------------------------------------------------- - <bean id="myJacksonModule" class="com.foo.MyModule"> - ... // configure the module as you want - </bean> - - <dataFormats> - <jacksonxml id="jacksonxml" useList="true" unmarshalTypeName="com.foo.MyPojo" moduleRefs="myJacksonModule"/> - </dataFormats> ------------------------------------------------------------------------------------------------------------------- - - Multiple modules can be specified separated by comma, such as -moduleRefs="myJacksonModule,myOtherModule" - -== Enabling or disable features using Jackson - -Jackson has a number of features you can enable or disable, which its -ObjectMapper uses. For example to disable failing on unknown properties -when marshalling, you can configure this using the disableFeatures: - -[source,java] -------------------------------------------------------------------------------------------------------------------- - <dataFormats> - <jacksonxml id="jacksonxml" unmarshalTypeName="com.foo.MyPojo" disableFeatures="FAIL_ON_UNKNOWN_PROPERTIES"/> - </dataFormats> -------------------------------------------------------------------------------------------------------------------- - -You can disable multiple features by separating the values using comma. -The values for the features must be the name of the enums from Jackson -from the following enum classes - -* com.fasterxml.jackson.databind.SerializationFeature -* com.fasterxml.jackson.databind.DeserializationFeature -* com.fasterxml.jackson.databind.MapperFeature - -To enable a feature use the enableFeatures options instead. - -From Java code you can use the type safe methods from camel-jackson -module: - -[source,java] ----------------------------------------------------------------------- -JacksonDataFormat df = new JacksonDataFormat(MyPojo.class); -df.disableFeature(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); -df.disableFeature(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES); ----------------------------------------------------------------------- - -== Converting Maps to POJO using Jackson - -Jackson `ObjectMapper` can be used to convert maps to POJO objects. -Jackson component comes with the data converter that can be used to -convert `java.util.Map` instance to non-String, non-primitive and -non-Number objects. - -[source,java] ----------------------------------------------------------------- -Map<String, Object> invoiceData = new HashMap<String, Object>(); -invoiceData.put("netValue", 500); -producerTemplate.sendBody("direct:mapToInvoice", invoiceData); -... -// Later in the processor -Invoice invoice = exchange.getIn().getBody(Invoice.class); ----------------------------------------------------------------- - -If there is a single `ObjectMapper` instance available in the Camel -registry, it will used by the converter to perform the conversion. -Otherwise the default mapper will be used. - -== Formatted XML marshalling (pretty-printing) - -Using the `prettyPrint` option one can output a well formatted XML while -marshalling: - -[source,java] ------------------------------------------------- - <dataFormats> - <jacksonxml id="jack" prettyPrint="true"/> - </dataFormats> ------------------------------------------------- - -And in Java DSL: - -[source,java] ---------------------------------------------------- -from("direct:inPretty").marshal().jacksonxml(true); ---------------------------------------------------- - -Please note that there are 5 different overloaded `jacksonxml()` DSL -methods which support the `prettyPrint` option in combination with other -settings for `unmarshalType`, `jsonView` etc. +If you setup a single `AvroMapper` in the registry, then Camel will automatic lookup and use this `AvroMapper`. == Dependencies -To use Jackson XML in your camel routes you need to add the dependency -on *camel-jacksonxml* which implements this data format. +To use Avro Jackson in your camel routes you need to add the dependency +on *camel-jackson-avro* 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 @@ -365,10 +86,8 @@ the download page for the latest versions). ---------------------------------------------------------- <dependency> <groupId>org.apache.camel</groupId> - <artifactId>camel-jacksonxml</artifactId> + <artifactId>camel-jackson-avro</artifactId> <version>x.x.x</version> <!-- use the same version as your Camel core version --> </dependency> ---------------------------------------------------------- - -include::{page-component-version}@camel-spring-boot::page$jacksonxml-starter.adoc[] diff --git a/components/camel-jackson-protobuf/src/main/docs/protobuf-jackson-dataformat.adoc b/components/camel-jackson-protobuf/src/main/docs/protobuf-jackson-dataformat.adoc index d486c52..7f6136f 100644 --- a/components/camel-jackson-protobuf/src/main/docs/protobuf-jackson-dataformat.adoc +++ b/components/camel-jackson-protobuf/src/main/docs/protobuf-jackson-dataformat.adoc @@ -1,45 +1,41 @@ [[protobuf-jackson-dataformat]] -= Protobuf DataFormat -:docTitle: Protobuf += Protobuf Jackson DataFormat +:docTitle: Protobuf Jackson :artifactId: camel-jackson-protobuf :description: Marshal POJOs to Protobuf and back using Jackson -:since: 2.2 +:since: 3.10 :supportLevel: Stable -include::{cq-version}@camel-quarkus:ROOT:partial$reference/dataformats/protobuf-jackson.adoc[opts=optional] *Since Camel {since}* -Jackson XML is a Data Format which uses the -http://wiki.fasterxml.com/JacksonHome/[Jackson library] with the -https://github.com/FasterXML/jackson-dataformat-xml[XMLMapper extension] -to unmarshal an XML payload into Java objects or to marshal Java objects -into an XML payload. +Jackson Protobuf is a Data Format which uses the +http://wiki.fasterxml.com/JacksonHome/[Jackson library] with the https://github.com/FasterXML/jackson-dataformats-binary[Protobuf extension] +to unmarshal a Protobuf payload into Java objects or to marshal Java objects +into a Protobuf payload. [TIP] ==== -If you are familiar with Jackson, this XML data format behaves in the +If you are familiar with Jackson, this Protobuf data format behaves in the same way as its JSON counterpart, and thus can be used with classes annotated for JSON serialization/deserialization. ==== -This extension also mimics -https://github.com/FasterXML/jackson-dataformat-xml/blob/master/README.md[JAXB's -"Code first" approach]. - -This data format relies on -http://wiki.fasterxml.com/WoodstoxHome[Woodstox] (especially for -features like pretty printing), a fast and efficient XML processor. - [source,java] ------------------------------- -from("activemq:My.Queue"). - unmarshal().jacksonxml(). - to("mqseries:Another.Queue"); +from("kafka:topic"). + unmarshal().protobuf(ProtobufLibrary.Jackson, JsonNode.class). + to("log:info"); ------------------------------- -== JacksonXML Options +== Configuring the SchemaResolver + +Since Protobuf serialization is schema-based, this data format requires that you provide a SchemaResolver object +that is able to lookup the schema for each exchange that is going to be marshalled/unmarshalled. +You can add a single SchemaResolver to the registry and it will be looked up automatically. +Or you can explicitly specify the reference to a custom SchemaResolver. +== Protobuf Jackson Options // dataformat options: START The Protobuf dataformat supports 18 options, which are listed below. @@ -71,291 +67,16 @@ The Protobuf dataformat supports 18 options, which are listed below. // dataformat options: END -=== Using Jackson XML 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. - -[source,xml] ------------------------------------------------------------------------------------------------------------------------------ - <dataFormats> - <!-- here we define a Xml 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 --> - <jacksonxml id="jack" unmarshalTypeName="org.apache.camel.component.jacksonxml.TestPojo"/> - </dataFormats> ------------------------------------------------------------------------------------------------------------------------------ - -And then you can refer to this id in the route: - -[source,xml] -------------------------------------- - <route> - <from uri="direct:back"/> - <unmarshal><custom ref="jack"/></unmarshal> - <to uri="mock:reverse"/> - </route> -------------------------------------- - -== Excluding POJO fields from marshalling - -When marshalling a POJO to XML you might want to exclude certain fields -from the XML output. With Jackson you can -use http://wiki.fasterxml.com/JacksonJsonViews[JSON views] to accomplish -this. First create one or more marker classes. - -Use the marker classes with the `@JsonView` annotation to -include/exclude certain fields. The annotation also works on getters. - -Finally use the Camel `JacksonXMLDataFormat` to marshall the above POJO -to XML. - -Note that the weight field is missing in the resulting XML: - -[source,java] ----------------------------- -<pojo age="30" weight="70"/> ----------------------------- - -== Include/Exclude fields using the `jsonView` attribute with `JacksonXML`DataFormat - -As an example of using this attribute you can instead of: - -[source,java] ---------------------------------------------------------------------------------------------------- -JacksonXMLDataFormat ageViewFormat = new JacksonXMLDataFormat(TestPojoView.class, Views.Age.class); -from("direct:inPojoAgeView"). - marshal(ageViewFormat); ---------------------------------------------------------------------------------------------------- - -Directly specify your http://wiki.fasterxml.com/JacksonJsonViews[JSON -view] inside the Java DSL as: - -[source,java] ------------------------------------------------------------- -from("direct:inPojoAgeView"). - marshal().jacksonxml(TestPojoView.class, Views.Age.class); ------------------------------------------------------------- - -And the same in XML DSL: - -[source,xml] ---------------------------------------------------------------------------------------------------------------------------------------------------- -<from uri="direct:inPojoAgeView"/> - <marshal> - <jacksonxml unmarshalTypeName="org.apache.camel.component.jacksonxml.TestPojoView" jsonView="org.apache.camel.component.jacksonxml.Views$Age"/> - </marshal> ---------------------------------------------------------------------------------------------------------------------------------------------------- - -== Setting serialization include option - -If you want to marshal a pojo to XML, 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, - -[source,java] ------------------------------- -@JsonInclude(Include.NON_NULL) -public class MyPojo { - ... -} ------------------------------- - -But this requires you to include that annotation in your pojo source -code. You can also configure the Camel JacksonXMLDataFormat to set the -include option, as shown below: - -[source,java] ---------------------------------------------------------- -JacksonXMLDataFormat format = new JacksonXMLDataFormat(); -format.setInclude("NON_NULL"); ---------------------------------------------------------- - -Or from XML DSL you configure this as - -[source,java] ------------------------------------------------------- - <dataFormats> - <jacksonxml id="jacksonxml" include="NON_NULL"/> - </dataFormats> ------------------------------------------------------- - -== Unmarshalling from XML to POJO with dynamic class name - -If you use jackson to unmarshal XML 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 XML payload as. - - 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: - -[source,java] ---------------------------------------------------- -JacksonDataFormat format = new JacksonDataFormat(); -format.setAllowJmsType(true); ---------------------------------------------------- - -Or from XML DSL you configure this as - -[source,java] -------------------------------------------------------- - <dataFormats> - <jacksonxml id="jacksonxml" allowJmsType="true"/> - </dataFormats> -------------------------------------------------------- - -== Unmarshalling from XML to List<Map> or List<pojo> - -If you are using Jackson to unmarshal XML to a list of map/pojo, you can -now specify this by setting `useList="true"` or use -the `org.apache.camel.component.jacksonxml.ListJacksonXMLDataFormat`. -For example with Java you can do as shown below: - -[source,java] -------------------------------------------------------------- -JacksonXMLDataFormat format = new ListJacksonXMLDataFormat(); -// or -JacksonXMLDataFormat format = new JacksonXMLDataFormat(); -format.useList(); -// and you can specify the pojo class type also -format.setUnmarshalType(MyPojo.class); -------------------------------------------------------------- - -And if you use XML DSL then you configure to use list -using `useList` attribute as shown below: - -[source,java] --------------------------------------------- - <dataFormats> - <jacksonxml id="jack" useList="true"/> - </dataFormats> --------------------------------------------- - -And you can specify the pojo type also - -[source,java] -------------------------------------------------------------------------------- - <dataFormats> - <jacksonxml id="jack" useList="true" unmarshalTypeName="com.foo.MyPojo"/> - </dataFormats> -------------------------------------------------------------------------------- - -== Using custom Jackson modules - -You can use custom Jackson modules by specifying the class names of -those using the moduleClassNames option as shown below. - -[source,java] ------------------------------------------------------------------------------------------------------------------------------------------ - <dataFormats> - <jacksonxml id="jack" useList="true" unmarshalTypeName="com.foo.MyPojo" moduleClassNames="com.foo.MyModule,com.foo.MyOtherModule"/> - </dataFormats> ------------------------------------------------------------------------------------------------------------------------------------------ - -When using moduleClassNames then the custom jackson modules are not -configured, by created using default constructor and used as-is. If a -custom module needs any custom configuration, then an instance of the -module can be created and configured, and then use modulesRefs to refer -to the module as shown below: - -[source,java] ------------------------------------------------------------------------------------------------------------------- - <bean id="myJacksonModule" class="com.foo.MyModule"> - ... // configure the module as you want - </bean> - - <dataFormats> - <jacksonxml id="jacksonxml" useList="true" unmarshalTypeName="com.foo.MyPojo" moduleRefs="myJacksonModule"/> - </dataFormats> ------------------------------------------------------------------------------------------------------------------- - - Multiple modules can be specified separated by comma, such as -moduleRefs="myJacksonModule,myOtherModule" - -== Enabling or disable features using Jackson +== Using custom ProtobufMapper -Jackson has a number of features you can enable or disable, which its -ObjectMapper uses. For example to disable failing on unknown properties -when marshalling, you can configure this using the disableFeatures: +You can configure `JacksonProtobufDataFormat` to use a custom `ProtobufMapper` in case you need more control of the mapping configuration. -[source,java] -------------------------------------------------------------------------------------------------------------------- - <dataFormats> - <jacksonxml id="jacksonxml" unmarshalTypeName="com.foo.MyPojo" disableFeatures="FAIL_ON_UNKNOWN_PROPERTIES"/> - </dataFormats> -------------------------------------------------------------------------------------------------------------------- - -You can disable multiple features by separating the values using comma. -The values for the features must be the name of the enums from Jackson -from the following enum classes - -* com.fasterxml.jackson.databind.SerializationFeature -* com.fasterxml.jackson.databind.DeserializationFeature -* com.fasterxml.jackson.databind.MapperFeature - -To enable a feature use the enableFeatures options instead. - -From Java code you can use the type safe methods from camel-jackson -module: - -[source,java] ----------------------------------------------------------------------- -JacksonDataFormat df = new JacksonDataFormat(MyPojo.class); -df.disableFeature(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); -df.disableFeature(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES); ----------------------------------------------------------------------- - -== Converting Maps to POJO using Jackson - -Jackson `ObjectMapper` can be used to convert maps to POJO objects. -Jackson component comes with the data converter that can be used to -convert `java.util.Map` instance to non-String, non-primitive and -non-Number objects. - -[source,java] ----------------------------------------------------------------- -Map<String, Object> invoiceData = new HashMap<String, Object>(); -invoiceData.put("netValue", 500); -producerTemplate.sendBody("direct:mapToInvoice", invoiceData); -... -// Later in the processor -Invoice invoice = exchange.getIn().getBody(Invoice.class); ----------------------------------------------------------------- - -If there is a single `ObjectMapper` instance available in the Camel -registry, it will used by the converter to perform the conversion. -Otherwise the default mapper will be used. - -== Formatted XML marshalling (pretty-printing) - -Using the `prettyPrint` option one can output a well formatted XML while -marshalling: - -[source,java] ------------------------------------------------- - <dataFormats> - <jacksonxml id="jack" prettyPrint="true"/> - </dataFormats> ------------------------------------------------- - -And in Java DSL: - -[source,java] ---------------------------------------------------- -from("direct:inPretty").marshal().jacksonxml(true); ---------------------------------------------------- - -Please note that there are 5 different overloaded `jacksonxml()` DSL -methods which support the `prettyPrint` option in combination with other -settings for `unmarshalType`, `jsonView` etc. +If you setup a single `ProtobufMapper` in the registry, then Camel will automatic lookup and use this `ProtobufMapper`. == Dependencies -To use Jackson XML in your camel routes you need to add the dependency -on *camel-jacksonxml* which implements this data format. +To use Protobuf Jackson in your camel routes you need to add the dependency +on *camel-jackson-protobuf* 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 @@ -365,10 +86,8 @@ the download page for the latest versions). ---------------------------------------------------------- <dependency> <groupId>org.apache.camel</groupId> - <artifactId>camel-jacksonxml</artifactId> + <artifactId>camel-jackson-protobuf</artifactId> <version>x.x.x</version> <!-- use the same version as your Camel core version --> </dependency> ---------------------------------------------------------- - -include::{page-component-version}@camel-spring-boot::page$jacksonxml-starter.adoc[] diff --git a/docs/components/modules/dataformats/nav.adoc b/docs/components/modules/dataformats/nav.adoc index 135f439..6075ac5 100644 --- a/docs/components/modules/dataformats/nav.adoc +++ b/docs/components/modules/dataformats/nav.adoc @@ -5,7 +5,7 @@ ** xref:dataformats:any23-dataformat.adoc[Any23] ** xref:dataformats:asn1-dataformat.adoc[ASN.1 File] ** xref:dataformats:avro-dataformat.adoc[Avro] -** xref:dataformats:avro-jackson-dataformat.adoc[Avro] +** xref:dataformats:avro-jackson-dataformat.adoc[Avro Jackson] ** xref:dataformats:barcode-dataformat.adoc[Barcode] ** xref:dataformats:base64-dataformat.adoc[Base64] ** xref:dataformats:beanio-dataformat.adoc[BeanIO] @@ -33,7 +33,7 @@ ** xref:dataformats:mime-multipart-dataformat.adoc[MIME Multipart] ** xref:dataformats:pgp-dataformat.adoc[PGP] ** xref:dataformats:protobuf-dataformat.adoc[Protobuf] -** xref:dataformats:protobuf-jackson-dataformat.adoc[Protobuf] +** xref:dataformats:protobuf-jackson-dataformat.adoc[Protobuf Jackson] ** xref:dataformats:rss-dataformat.adoc[RSS] ** xref:dataformats:soapjaxb-dataformat.adoc[SOAP] ** xref:dataformats:syslog-dataformat.adoc[Syslog] diff --git a/docs/components/modules/dataformats/pages/avro-jackson-dataformat.adoc b/docs/components/modules/dataformats/pages/avro-jackson-dataformat.adoc index b130656..ba51c0f 100644 --- a/docs/components/modules/dataformats/pages/avro-jackson-dataformat.adoc +++ b/docs/components/modules/dataformats/pages/avro-jackson-dataformat.adoc @@ -1,47 +1,43 @@ [[avro-jackson-dataformat]] -= Avro DataFormat += Avro Jackson DataFormat //THIS FILE IS COPIED: EDIT THE SOURCE FILE: :page-source: components/camel-jackson-avro/src/main/docs/avro-jackson-dataformat.adoc -:docTitle: Avro +:docTitle: Avro Jackson :artifactId: camel-jackson-avro :description: Marshal POJOs to Avro and back using Jackson -:since: 2.14 +:since: 3.10 :supportLevel: Stable -include::{cq-version}@camel-quarkus:ROOT:partial$reference/dataformats/avro-jackson.adoc[opts=optional] *Since Camel {since}* -Jackson XML is a Data Format which uses the -http://wiki.fasterxml.com/JacksonHome/[Jackson library] with the -https://github.com/FasterXML/jackson-dataformat-xml[XMLMapper extension] -to unmarshal an XML payload into Java objects or to marshal Java objects -into an XML payload. +Jackson Avro is a Data Format which uses the +http://wiki.fasterxml.com/JacksonHome/[Jackson library] with the https://github.com/FasterXML/jackson-dataformats-binary[Avro extension] +to unmarshal an Avro payload into Java objects or to marshal Java objects +into an Avro payload. [TIP] ==== -If you are familiar with Jackson, this XML data format behaves in the +If you are familiar with Jackson, this Avro data format behaves in the same way as its JSON counterpart, and thus can be used with classes annotated for JSON serialization/deserialization. ==== -This extension also mimics -https://github.com/FasterXML/jackson-dataformat-xml/blob/master/README.md[JAXB's -"Code first" approach]. - -This data format relies on -http://wiki.fasterxml.com/WoodstoxHome[Woodstox] (especially for -features like pretty printing), a fast and efficient XML processor. - [source,java] ------------------------------- -from("activemq:My.Queue"). - unmarshal().jacksonxml(). - to("mqseries:Another.Queue"); +from("kafka:topic"). + unmarshal().avro(AvroLibrary.Jackson, JsonNode.class). + to("log:info"); ------------------------------- -== JacksonXML Options +== Configuring the SchemaResolver + +Since Avro serialization is schema-based, this data format requires that you provide a SchemaResolver object +that is able to lookup the schema for each exchange that is going to be marshalled/unmarshalled. +You can add a single SchemaResolver to the registry and it will be looked up automatically. +Or you can explicitly specify the reference to a custom SchemaResolver. +== Avro Jackson Options // dataformat options: START The Avro dataformat supports 18 options, which are listed below. @@ -51,6 +47,7 @@ The Avro dataformat supports 18 options, which are listed below. [width="100%",cols="2s,1m,1m,6",options="header"] |=== | Name | Default | Java Type | Description +| contentTypeHeader | true | Boolean | 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 | objectMapper | | String | Lookup and use the existing ObjectMapper with the given id when using Jackson. | useDefaultObjectMapper | true | Boolean | Whether to lookup and use default Jackson ObjectMapper from the registry. | unmarshalTypeName | | String | Class name of the java type to use when unmarshalling @@ -66,298 +63,22 @@ The Avro dataformat supports 18 options, which are listed below. | allowUnmarshallType | 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. | timezone | | String | If set then Jackson will use the Timezone when marshalling/unmarshalling. | autoDiscoverObjectMapper | false | Boolean | If set to true then Jackson will lookup for an objectMapper into the registry -| contentTypeHeader | true | Boolean | 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 | schemaResolver | | String | Optional schema resolver used to lookup schemas for the data in transit. | autoDiscoverSchemaResolver | true | Boolean | When not disabled, the SchemaResolver will be looked up into the registry |=== // dataformat options: END -=== Using Jackson XML 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. - -[source,xml] ------------------------------------------------------------------------------------------------------------------------------ - <dataFormats> - <!-- here we define a Xml 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 --> - <jacksonxml id="jack" unmarshalTypeName="org.apache.camel.component.jacksonxml.TestPojo"/> - </dataFormats> ------------------------------------------------------------------------------------------------------------------------------ - -And then you can refer to this id in the route: - -[source,xml] -------------------------------------- - <route> - <from uri="direct:back"/> - <unmarshal><custom ref="jack"/></unmarshal> - <to uri="mock:reverse"/> - </route> -------------------------------------- - -== Excluding POJO fields from marshalling - -When marshalling a POJO to XML you might want to exclude certain fields -from the XML output. With Jackson you can -use http://wiki.fasterxml.com/JacksonJsonViews[JSON views] to accomplish -this. First create one or more marker classes. - -Use the marker classes with the `@JsonView` annotation to -include/exclude certain fields. The annotation also works on getters. - -Finally use the Camel `JacksonXMLDataFormat` to marshall the above POJO -to XML. - -Note that the weight field is missing in the resulting XML: - -[source,java] ----------------------------- -<pojo age="30" weight="70"/> ----------------------------- - -== Include/Exclude fields using the `jsonView` attribute with `JacksonXML`DataFormat - -As an example of using this attribute you can instead of: - -[source,java] ---------------------------------------------------------------------------------------------------- -JacksonXMLDataFormat ageViewFormat = new JacksonXMLDataFormat(TestPojoView.class, Views.Age.class); -from("direct:inPojoAgeView"). - marshal(ageViewFormat); ---------------------------------------------------------------------------------------------------- - -Directly specify your http://wiki.fasterxml.com/JacksonJsonViews[JSON -view] inside the Java DSL as: - -[source,java] ------------------------------------------------------------- -from("direct:inPojoAgeView"). - marshal().jacksonxml(TestPojoView.class, Views.Age.class); ------------------------------------------------------------- - -And the same in XML DSL: - -[source,xml] ---------------------------------------------------------------------------------------------------------------------------------------------------- -<from uri="direct:inPojoAgeView"/> - <marshal> - <jacksonxml unmarshalTypeName="org.apache.camel.component.jacksonxml.TestPojoView" jsonView="org.apache.camel.component.jacksonxml.Views$Age"/> - </marshal> ---------------------------------------------------------------------------------------------------------------------------------------------------- - -== Setting serialization include option - -If you want to marshal a pojo to XML, 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, - -[source,java] ------------------------------- -@JsonInclude(Include.NON_NULL) -public class MyPojo { - ... -} ------------------------------- - -But this requires you to include that annotation in your pojo source -code. You can also configure the Camel JacksonXMLDataFormat to set the -include option, as shown below: - -[source,java] ---------------------------------------------------------- -JacksonXMLDataFormat format = new JacksonXMLDataFormat(); -format.setInclude("NON_NULL"); ---------------------------------------------------------- - -Or from XML DSL you configure this as - -[source,java] ------------------------------------------------------- - <dataFormats> - <jacksonxml id="jacksonxml" include="NON_NULL"/> - </dataFormats> ------------------------------------------------------- - -== Unmarshalling from XML to POJO with dynamic class name - -If you use jackson to unmarshal XML 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 XML payload as. - - 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: - -[source,java] ---------------------------------------------------- -JacksonDataFormat format = new JacksonDataFormat(); -format.setAllowJmsType(true); ---------------------------------------------------- - -Or from XML DSL you configure this as - -[source,java] -------------------------------------------------------- - <dataFormats> - <jacksonxml id="jacksonxml" allowJmsType="true"/> - </dataFormats> -------------------------------------------------------- - -== Unmarshalling from XML to List<Map> or List<pojo> - -If you are using Jackson to unmarshal XML to a list of map/pojo, you can -now specify this by setting `useList="true"` or use -the `org.apache.camel.component.jacksonxml.ListJacksonXMLDataFormat`. -For example with Java you can do as shown below: - -[source,java] -------------------------------------------------------------- -JacksonXMLDataFormat format = new ListJacksonXMLDataFormat(); -// or -JacksonXMLDataFormat format = new JacksonXMLDataFormat(); -format.useList(); -// and you can specify the pojo class type also -format.setUnmarshalType(MyPojo.class); -------------------------------------------------------------- - -And if you use XML DSL then you configure to use list -using `useList` attribute as shown below: - -[source,java] --------------------------------------------- - <dataFormats> - <jacksonxml id="jack" useList="true"/> - </dataFormats> --------------------------------------------- - -And you can specify the pojo type also +== Using custom AvroMapper -[source,java] -------------------------------------------------------------------------------- - <dataFormats> - <jacksonxml id="jack" useList="true" unmarshalTypeName="com.foo.MyPojo"/> - </dataFormats> -------------------------------------------------------------------------------- - -== Using custom Jackson modules - -You can use custom Jackson modules by specifying the class names of -those using the moduleClassNames option as shown below. +You can configure `JacksonAvroDataFormat` to use a custom `AvroMapper` in case you need more control of the mapping configuration. -[source,java] ------------------------------------------------------------------------------------------------------------------------------------------ - <dataFormats> - <jacksonxml id="jack" useList="true" unmarshalTypeName="com.foo.MyPojo" moduleClassNames="com.foo.MyModule,com.foo.MyOtherModule"/> - </dataFormats> ------------------------------------------------------------------------------------------------------------------------------------------ - -When using moduleClassNames then the custom jackson modules are not -configured, by created using default constructor and used as-is. If a -custom module needs any custom configuration, then an instance of the -module can be created and configured, and then use modulesRefs to refer -to the module as shown below: - -[source,java] ------------------------------------------------------------------------------------------------------------------- - <bean id="myJacksonModule" class="com.foo.MyModule"> - ... // configure the module as you want - </bean> - - <dataFormats> - <jacksonxml id="jacksonxml" useList="true" unmarshalTypeName="com.foo.MyPojo" moduleRefs="myJacksonModule"/> - </dataFormats> ------------------------------------------------------------------------------------------------------------------- - - Multiple modules can be specified separated by comma, such as -moduleRefs="myJacksonModule,myOtherModule" - -== Enabling or disable features using Jackson - -Jackson has a number of features you can enable or disable, which its -ObjectMapper uses. For example to disable failing on unknown properties -when marshalling, you can configure this using the disableFeatures: - -[source,java] -------------------------------------------------------------------------------------------------------------------- - <dataFormats> - <jacksonxml id="jacksonxml" unmarshalTypeName="com.foo.MyPojo" disableFeatures="FAIL_ON_UNKNOWN_PROPERTIES"/> - </dataFormats> -------------------------------------------------------------------------------------------------------------------- - -You can disable multiple features by separating the values using comma. -The values for the features must be the name of the enums from Jackson -from the following enum classes - -* com.fasterxml.jackson.databind.SerializationFeature -* com.fasterxml.jackson.databind.DeserializationFeature -* com.fasterxml.jackson.databind.MapperFeature - -To enable a feature use the enableFeatures options instead. - -From Java code you can use the type safe methods from camel-jackson -module: - -[source,java] ----------------------------------------------------------------------- -JacksonDataFormat df = new JacksonDataFormat(MyPojo.class); -df.disableFeature(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); -df.disableFeature(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES); ----------------------------------------------------------------------- - -== Converting Maps to POJO using Jackson - -Jackson `ObjectMapper` can be used to convert maps to POJO objects. -Jackson component comes with the data converter that can be used to -convert `java.util.Map` instance to non-String, non-primitive and -non-Number objects. - -[source,java] ----------------------------------------------------------------- -Map<String, Object> invoiceData = new HashMap<String, Object>(); -invoiceData.put("netValue", 500); -producerTemplate.sendBody("direct:mapToInvoice", invoiceData); -... -// Later in the processor -Invoice invoice = exchange.getIn().getBody(Invoice.class); ----------------------------------------------------------------- - -If there is a single `ObjectMapper` instance available in the Camel -registry, it will used by the converter to perform the conversion. -Otherwise the default mapper will be used. - -== Formatted XML marshalling (pretty-printing) - -Using the `prettyPrint` option one can output a well formatted XML while -marshalling: - -[source,java] ------------------------------------------------- - <dataFormats> - <jacksonxml id="jack" prettyPrint="true"/> - </dataFormats> ------------------------------------------------- - -And in Java DSL: - -[source,java] ---------------------------------------------------- -from("direct:inPretty").marshal().jacksonxml(true); ---------------------------------------------------- - -Please note that there are 5 different overloaded `jacksonxml()` DSL -methods which support the `prettyPrint` option in combination with other -settings for `unmarshalType`, `jsonView` etc. +If you setup a single `AvroMapper` in the registry, then Camel will automatic lookup and use this `AvroMapper`. == Dependencies -To use Jackson XML in your camel routes you need to add the dependency -on *camel-jacksonxml* which implements this data format. +To use Avro Jackson in your camel routes you need to add the dependency +on *camel-jackson-avro* 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 @@ -367,10 +88,8 @@ the download page for the latest versions). ---------------------------------------------------------- <dependency> <groupId>org.apache.camel</groupId> - <artifactId>camel-jacksonxml</artifactId> + <artifactId>camel-jackson-avro</artifactId> <version>x.x.x</version> <!-- use the same version as your Camel core version --> </dependency> ---------------------------------------------------------- - -include::{page-component-version}@camel-spring-boot::page$jacksonxml-starter.adoc[] diff --git a/docs/components/modules/dataformats/pages/protobuf-jackson-dataformat.adoc b/docs/components/modules/dataformats/pages/protobuf-jackson-dataformat.adoc index 7a608c4..01101cb 100644 --- a/docs/components/modules/dataformats/pages/protobuf-jackson-dataformat.adoc +++ b/docs/components/modules/dataformats/pages/protobuf-jackson-dataformat.adoc @@ -1,47 +1,43 @@ [[protobuf-jackson-dataformat]] -= Protobuf DataFormat += Protobuf Jackson DataFormat //THIS FILE IS COPIED: EDIT THE SOURCE FILE: :page-source: components/camel-jackson-protobuf/src/main/docs/protobuf-jackson-dataformat.adoc -:docTitle: Protobuf +:docTitle: Protobuf Jackson :artifactId: camel-jackson-protobuf :description: Marshal POJOs to Protobuf and back using Jackson -:since: 2.2 +:since: 3.10 :supportLevel: Stable -include::{cq-version}@camel-quarkus:ROOT:partial$reference/dataformats/protobuf-jackson.adoc[opts=optional] *Since Camel {since}* -Jackson XML is a Data Format which uses the -http://wiki.fasterxml.com/JacksonHome/[Jackson library] with the -https://github.com/FasterXML/jackson-dataformat-xml[XMLMapper extension] -to unmarshal an XML payload into Java objects or to marshal Java objects -into an XML payload. +Jackson Protobuf is a Data Format which uses the +http://wiki.fasterxml.com/JacksonHome/[Jackson library] with the https://github.com/FasterXML/jackson-dataformats-binary[Protobuf extension] +to unmarshal a Protobuf payload into Java objects or to marshal Java objects +into a Protobuf payload. [TIP] ==== -If you are familiar with Jackson, this XML data format behaves in the +If you are familiar with Jackson, this Protobuf data format behaves in the same way as its JSON counterpart, and thus can be used with classes annotated for JSON serialization/deserialization. ==== -This extension also mimics -https://github.com/FasterXML/jackson-dataformat-xml/blob/master/README.md[JAXB's -"Code first" approach]. - -This data format relies on -http://wiki.fasterxml.com/WoodstoxHome[Woodstox] (especially for -features like pretty printing), a fast and efficient XML processor. - [source,java] ------------------------------- -from("activemq:My.Queue"). - unmarshal().jacksonxml(). - to("mqseries:Another.Queue"); +from("kafka:topic"). + unmarshal().protobuf(ProtobufLibrary.Jackson, JsonNode.class). + to("log:info"); ------------------------------- -== JacksonXML Options +== Configuring the SchemaResolver + +Since Protobuf serialization is schema-based, this data format requires that you provide a SchemaResolver object +that is able to lookup the schema for each exchange that is going to be marshalled/unmarshalled. +You can add a single SchemaResolver to the registry and it will be looked up automatically. +Or you can explicitly specify the reference to a custom SchemaResolver. +== Protobuf Jackson Options // dataformat options: START The Protobuf dataformat supports 18 options, which are listed below. @@ -73,291 +69,16 @@ The Protobuf dataformat supports 18 options, which are listed below. // dataformat options: END -=== Using Jackson XML 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. - -[source,xml] ------------------------------------------------------------------------------------------------------------------------------ - <dataFormats> - <!-- here we define a Xml 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 --> - <jacksonxml id="jack" unmarshalTypeName="org.apache.camel.component.jacksonxml.TestPojo"/> - </dataFormats> ------------------------------------------------------------------------------------------------------------------------------ - -And then you can refer to this id in the route: - -[source,xml] -------------------------------------- - <route> - <from uri="direct:back"/> - <unmarshal><custom ref="jack"/></unmarshal> - <to uri="mock:reverse"/> - </route> -------------------------------------- - -== Excluding POJO fields from marshalling - -When marshalling a POJO to XML you might want to exclude certain fields -from the XML output. With Jackson you can -use http://wiki.fasterxml.com/JacksonJsonViews[JSON views] to accomplish -this. First create one or more marker classes. - -Use the marker classes with the `@JsonView` annotation to -include/exclude certain fields. The annotation also works on getters. - -Finally use the Camel `JacksonXMLDataFormat` to marshall the above POJO -to XML. - -Note that the weight field is missing in the resulting XML: - -[source,java] ----------------------------- -<pojo age="30" weight="70"/> ----------------------------- - -== Include/Exclude fields using the `jsonView` attribute with `JacksonXML`DataFormat - -As an example of using this attribute you can instead of: - -[source,java] ---------------------------------------------------------------------------------------------------- -JacksonXMLDataFormat ageViewFormat = new JacksonXMLDataFormat(TestPojoView.class, Views.Age.class); -from("direct:inPojoAgeView"). - marshal(ageViewFormat); ---------------------------------------------------------------------------------------------------- - -Directly specify your http://wiki.fasterxml.com/JacksonJsonViews[JSON -view] inside the Java DSL as: - -[source,java] ------------------------------------------------------------- -from("direct:inPojoAgeView"). - marshal().jacksonxml(TestPojoView.class, Views.Age.class); ------------------------------------------------------------- - -And the same in XML DSL: - -[source,xml] ---------------------------------------------------------------------------------------------------------------------------------------------------- -<from uri="direct:inPojoAgeView"/> - <marshal> - <jacksonxml unmarshalTypeName="org.apache.camel.component.jacksonxml.TestPojoView" jsonView="org.apache.camel.component.jacksonxml.Views$Age"/> - </marshal> ---------------------------------------------------------------------------------------------------------------------------------------------------- - -== Setting serialization include option - -If you want to marshal a pojo to XML, 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, - -[source,java] ------------------------------- -@JsonInclude(Include.NON_NULL) -public class MyPojo { - ... -} ------------------------------- - -But this requires you to include that annotation in your pojo source -code. You can also configure the Camel JacksonXMLDataFormat to set the -include option, as shown below: - -[source,java] ---------------------------------------------------------- -JacksonXMLDataFormat format = new JacksonXMLDataFormat(); -format.setInclude("NON_NULL"); ---------------------------------------------------------- - -Or from XML DSL you configure this as - -[source,java] ------------------------------------------------------- - <dataFormats> - <jacksonxml id="jacksonxml" include="NON_NULL"/> - </dataFormats> ------------------------------------------------------- - -== Unmarshalling from XML to POJO with dynamic class name - -If you use jackson to unmarshal XML 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 XML payload as. - - 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: - -[source,java] ---------------------------------------------------- -JacksonDataFormat format = new JacksonDataFormat(); -format.setAllowJmsType(true); ---------------------------------------------------- - -Or from XML DSL you configure this as - -[source,java] -------------------------------------------------------- - <dataFormats> - <jacksonxml id="jacksonxml" allowJmsType="true"/> - </dataFormats> -------------------------------------------------------- - -== Unmarshalling from XML to List<Map> or List<pojo> - -If you are using Jackson to unmarshal XML to a list of map/pojo, you can -now specify this by setting `useList="true"` or use -the `org.apache.camel.component.jacksonxml.ListJacksonXMLDataFormat`. -For example with Java you can do as shown below: - -[source,java] -------------------------------------------------------------- -JacksonXMLDataFormat format = new ListJacksonXMLDataFormat(); -// or -JacksonXMLDataFormat format = new JacksonXMLDataFormat(); -format.useList(); -// and you can specify the pojo class type also -format.setUnmarshalType(MyPojo.class); -------------------------------------------------------------- - -And if you use XML DSL then you configure to use list -using `useList` attribute as shown below: - -[source,java] --------------------------------------------- - <dataFormats> - <jacksonxml id="jack" useList="true"/> - </dataFormats> --------------------------------------------- - -And you can specify the pojo type also - -[source,java] -------------------------------------------------------------------------------- - <dataFormats> - <jacksonxml id="jack" useList="true" unmarshalTypeName="com.foo.MyPojo"/> - </dataFormats> -------------------------------------------------------------------------------- - -== Using custom Jackson modules - -You can use custom Jackson modules by specifying the class names of -those using the moduleClassNames option as shown below. - -[source,java] ------------------------------------------------------------------------------------------------------------------------------------------ - <dataFormats> - <jacksonxml id="jack" useList="true" unmarshalTypeName="com.foo.MyPojo" moduleClassNames="com.foo.MyModule,com.foo.MyOtherModule"/> - </dataFormats> ------------------------------------------------------------------------------------------------------------------------------------------ - -When using moduleClassNames then the custom jackson modules are not -configured, by created using default constructor and used as-is. If a -custom module needs any custom configuration, then an instance of the -module can be created and configured, and then use modulesRefs to refer -to the module as shown below: - -[source,java] ------------------------------------------------------------------------------------------------------------------- - <bean id="myJacksonModule" class="com.foo.MyModule"> - ... // configure the module as you want - </bean> - - <dataFormats> - <jacksonxml id="jacksonxml" useList="true" unmarshalTypeName="com.foo.MyPojo" moduleRefs="myJacksonModule"/> - </dataFormats> ------------------------------------------------------------------------------------------------------------------- - - Multiple modules can be specified separated by comma, such as -moduleRefs="myJacksonModule,myOtherModule" - -== Enabling or disable features using Jackson +== Using custom ProtobufMapper -Jackson has a number of features you can enable or disable, which its -ObjectMapper uses. For example to disable failing on unknown properties -when marshalling, you can configure this using the disableFeatures: +You can configure `JacksonProtobufDataFormat` to use a custom `ProtobufMapper` in case you need more control of the mapping configuration. -[source,java] -------------------------------------------------------------------------------------------------------------------- - <dataFormats> - <jacksonxml id="jacksonxml" unmarshalTypeName="com.foo.MyPojo" disableFeatures="FAIL_ON_UNKNOWN_PROPERTIES"/> - </dataFormats> -------------------------------------------------------------------------------------------------------------------- - -You can disable multiple features by separating the values using comma. -The values for the features must be the name of the enums from Jackson -from the following enum classes - -* com.fasterxml.jackson.databind.SerializationFeature -* com.fasterxml.jackson.databind.DeserializationFeature -* com.fasterxml.jackson.databind.MapperFeature - -To enable a feature use the enableFeatures options instead. - -From Java code you can use the type safe methods from camel-jackson -module: - -[source,java] ----------------------------------------------------------------------- -JacksonDataFormat df = new JacksonDataFormat(MyPojo.class); -df.disableFeature(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); -df.disableFeature(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES); ----------------------------------------------------------------------- - -== Converting Maps to POJO using Jackson - -Jackson `ObjectMapper` can be used to convert maps to POJO objects. -Jackson component comes with the data converter that can be used to -convert `java.util.Map` instance to non-String, non-primitive and -non-Number objects. - -[source,java] ----------------------------------------------------------------- -Map<String, Object> invoiceData = new HashMap<String, Object>(); -invoiceData.put("netValue", 500); -producerTemplate.sendBody("direct:mapToInvoice", invoiceData); -... -// Later in the processor -Invoice invoice = exchange.getIn().getBody(Invoice.class); ----------------------------------------------------------------- - -If there is a single `ObjectMapper` instance available in the Camel -registry, it will used by the converter to perform the conversion. -Otherwise the default mapper will be used. - -== Formatted XML marshalling (pretty-printing) - -Using the `prettyPrint` option one can output a well formatted XML while -marshalling: - -[source,java] ------------------------------------------------- - <dataFormats> - <jacksonxml id="jack" prettyPrint="true"/> - </dataFormats> ------------------------------------------------- - -And in Java DSL: - -[source,java] ---------------------------------------------------- -from("direct:inPretty").marshal().jacksonxml(true); ---------------------------------------------------- - -Please note that there are 5 different overloaded `jacksonxml()` DSL -methods which support the `prettyPrint` option in combination with other -settings for `unmarshalType`, `jsonView` etc. +If you setup a single `ProtobufMapper` in the registry, then Camel will automatic lookup and use this `ProtobufMapper`. == Dependencies -To use Jackson XML in your camel routes you need to add the dependency -on *camel-jacksonxml* which implements this data format. +To use Protobuf Jackson in your camel routes you need to add the dependency +on *camel-jackson-protobuf* 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 @@ -367,10 +88,8 @@ the download page for the latest versions). ---------------------------------------------------------- <dependency> <groupId>org.apache.camel</groupId> - <artifactId>camel-jacksonxml</artifactId> + <artifactId>camel-jackson-protobuf</artifactId> <version>x.x.x</version> <!-- use the same version as your Camel core version --> </dependency> ---------------------------------------------------------- - -include::{page-component-version}@camel-spring-boot::page$jacksonxml-starter.adoc[]