| [[DataFormat-DataFormat]] |
| = Data Format |
| |
| Camel supports a pluggable DataFormat to allow messages to be marshalled |
| to and from binary or text formats to support a kind of |
| xref:message-translator.adoc[Message Translator]. |
| |
| The following data formats are currently supported: |
| |
| * Object marshalling |
| ** xref:components::avro-dataformat.adoc[Avro] |
| ** xref:json.adoc[JSON] |
| ** xref:components::protobuf-dataformat.adoc[Protobuf] |
| ** xref:components::yaml-snakeyaml-dataformat.adoc[YAML] |
| |
| * Object/XML marshalling |
| ** xref:components::jaxb-dataformat.adoc[JAXB] |
| ** xref:components::xstream-dataformat.adoc[XStream] |
| ** xref:components::jacksonxml-dataformat.adoc[Jackson XML] |
| |
| * Flat data structure marshalling |
| ** xref:components::beanio-dataformat.adoc[BeanIO] |
| ** xref:components::bindy-dataformat.adoc[Bindy] |
| ** xref:components::csv-dataformat.adoc[CSV] |
| ** xref:components::flatpack-dataformat.adoc[Flatpack DataFormat] |
| ** uniVocity DataFormats xref:components::univocity-csv-dataformat.adoc[CSV] / xref:components::univocity-tsv-dataformat.adoc[TSV] / xref:components::univocity-fixed-dataformat.adoc[Fixed Length] |
| |
| * Domain specific marshalling |
| ** xref:components::hl7-dataformat.adoc[HL7 DataFormat] |
| |
| * Compression |
| ** xref:components::zipfile-dataformat.adoc[Zip File DataFormat] |
| ** xref:components::lzf-dataformat.adoc[LZF Data Format] |
| |
| * Security |
| ** xref:components::crypto-component.adoc[Crypto] |
| ** xref:components::crypto-component.adoc[PGP] |
| ** xref:components::secureXML-dataformat.adoc[XMLSecurity DataFormat] |
| |
| * Misc. |
| ** xref:components::base64-dataformat.adoc[Base64] |
| ** xref:components::mime-multipart-dataformat.adoc[MIME-Multipart] |
| ** xref:components::rss-dataformat.adoc[RSS] |
| ** xref:components::syslog-dataformat.adoc[Syslog] |
| ** xref:components::ical-dataformat.adoc[ICal] |
| ** xref:components::barcode-dataformat.adoc[Barcode] -- to read and generate barcodes |
| (QR-Code, PDF417, ...) |
| |
| And related is the following: |
| |
| * xref:components::dataformat-component.adoc[DataFormat Component] for working with |
| Data Formats as if it was a regular xref:component.adoc[Component] |
| supporting xref:endpoint.adoc[Endpoints] and xref:uris.adoc[URIs]. |
| * xref:dozer-type-conversion.adoc[Dozer Type Conversion] using Dozer for |
| type converting POJOs |
| |
| [[DataFormat-Unmarshalling]] |
| == Unmarshalling |
| |
| If you receive a message from one of the Camel |
| xref:component.adoc[Components] such as xref:components::file-component.adoc[File], |
| xref:components::http-component.adoc[HTTP] or xref:components::jms-component.adoc[JMS] you often want to unmarshal |
| the payload into some bean so that you can process it using some |
| xref:bean-integration.adoc[Bean Integration] or perform |
| xref:predicate.adoc[Predicate] evaluation and so forth. To do this use |
| the `unmarshal` word in the xref:dsl.adoc[DSL] in Java or the |
| xref:xml-configuration.adoc[XML Configuration]. |
| |
| For example: |
| |
| [source,java] |
| ---- |
| DataFormat jaxb = new JaxbDataFormat("com.acme.model"); |
| |
| from("activemq:My.Queue"). |
| unmarshal(jaxb). |
| to("mqseries:Another.Queue"); |
| ---- |
| |
| The above uses a named DataFormat of `jaxb` which is configured with a |
| number of Java package names. You can if you prefer use a named |
| reference to a data format which can then be defined in your |
| xref:registry.adoc[Registry] such as via your xref:spring.adoc[Spring] |
| XML file. |
| |
| You can also use the DSL itself to define the data format as you use it. |
| For example the following uses Java serialization to unmarshal a binary |
| file then send it as an ObjectMessage to xref:components::activemq-component.adoc[ActiveMQ] |
| |
| [source,java] |
| ---- |
| from("file://foo/bar"). |
| unmarshal().serialization(). |
| to("activemq:Some.Queue"); |
| ---- |
| |
| [[DataFormat-Marshalling]] |
| == Marshalling |
| |
| Marshalling is the opposite of unmarshalling, where a bean is marshalled |
| into some binary or textual format for transmission over some transport |
| via a Camel xref:component.adoc[Component]. Marshalling is used in the |
| same way as unmarshalling above; in the xref:dsl.adoc[DSL] you can use a |
| DataFormat instance, you can configure the DataFormat dynamically using |
| the DSL or you can refer to a named instance of the format in the |
| xref:registry.adoc[Registry]. |
| |
| The following example unmarshals via serialization then marshals using a |
| named JAXB data format to perform a kind of |
| xref:message-translator.adoc[Message Translator]: |
| |
| [source,java] |
| ---- |
| from("file://foo/bar"). |
| unmarshal().serialization(). |
| marshal("jaxb"). |
| to("activemq:Some.Queue"); |
| ---- |
| |
| [[DataFormat-UsingSpringXML]] |
| == Using Spring XML |
| |
| This example shows how to configure the data type just once and reuse it |
| on multiple routes: |
| |
| [source,xml] |
| ---- |
| <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring"> |
| <dataFormats> |
| <jaxb id="myJaxb" prettyPrint="true" contextPath="org.apache.camel.example"/> |
| </dataFormats> |
| |
| <route> |
| <from uri="direct:start"/> |
| <marshal ref="myJaxb"/> |
| <to uri="direct:marshalled"/> |
| </route> |
| <route> |
| <from uri="direct:marshalled"/> |
| <unmarshal ref="myJaxb"/> |
| <to uri="mock:result"/> |
| </route> |
| |
| </camelContext> |
| ---- |
| |
| You can also define reusable data formats as Spring beans: |
| |
| [source,xml] |
| ---- |
| <bean id="myJaxb" class="org.apache.camel.model.dataformat.JaxbDataFormat"> |
| <property name="prettyPrint" value="true"/> |
| <property name="contextPath" value="org.apache.camel.example"/> |
| </bean> |
| ---- |