Questo contenuto non è disponibile nella lingua selezionata.
Chapter 380. YAML SnakeYAML DataFormat
Available as of Camel version 2.17
YAML is a Data Format to marshal and unmarshal Java objects to and from YAML.
For YAML to object marshalling, Camel provides integration with three popular YAML libraries:
- The SnakeYAML library
Every library requires adding the special camel component (see "Dependency…" paragraphs further down). By default Camel uses the SnakeYAML library.
380.1. YAML Options Copia collegamentoCollegamento copiato negli appunti!
The YAML SnakeYAML dataformat supports 11 options, which are listed below.
Name | Default | Java Type | Description |
---|---|---|---|
library |
|
| Which yaml library to use. By default it is SnakeYAML |
unmarshalTypeName |
| Class name of the java type to use when unarmshalling | |
constructor |
| BaseConstructor to construct incoming documents. | |
representer |
| Representer to emit outgoing objects. | |
dumperOptions |
| DumperOptions to configure outgoing objects. | |
resolver |
| Resolver to detect implicit type | |
useApplicationContextClassLoader |
|
| Use ApplicationContextClassLoader as custom ClassLoader |
prettyFlow |
|
| Force the emitter to produce a pretty YAML document when using the flow style. |
allowAnyType |
|
| Allow any class to be un-marshaled |
typeFilter |
| Set the types SnakeYAML is allowed to un-marshall | |
contentTypeHeader |
|
| Whether the data format should set the Content-Type header with the type from the data format if the data format is capable of doing so. For example application/xml for data formats marshalling to XML, or application/json for data formats marshalling to JSon etc. |
380.2. Spring Boot Auto-Configuration Copia collegamentoCollegamento copiato negli appunti!
The component supports 12 options, which are listed below.
Name | Description | Default | Type |
---|---|---|---|
camel.dataformat.yaml-snakeyaml.allow-any-type | Allow any class to be un-marshaled | false | Boolean |
camel.dataformat.yaml-snakeyaml.constructor | BaseConstructor to construct incoming documents. | String | |
camel.dataformat.yaml-snakeyaml.content-type-header | Whether the data format should set the Content-Type header with the type from the data format if the data format is capable of doing so. For example application/xml for data formats marshalling to XML, or application/json for data formats marshalling to JSon etc. | false | Boolean |
camel.dataformat.yaml-snakeyaml.dumper-options | DumperOptions to configure outgoing objects. | String | |
camel.dataformat.yaml-snakeyaml.enabled | Enable yaml-snakeyaml dataformat | true | Boolean |
camel.dataformat.yaml-snakeyaml.library | Which yaml library to use. By default it is SnakeYAML | YAMLLibrary | |
camel.dataformat.yaml-snakeyaml.pretty-flow | Force the emitter to produce a pretty YAML document when using the flow style. | false | Boolean |
camel.dataformat.yaml-snakeyaml.representer | Representer to emit outgoing objects. | String | |
camel.dataformat.yaml-snakeyaml.resolver | Resolver to detect implicit type | String | |
camel.dataformat.yaml-snakeyaml.type-filter | Set the types SnakeYAML is allowed to un-marshall | List | |
camel.dataformat.yaml-snakeyaml.unmarshal-type-name | Class name of the java type to use when unarmshalling | String | |
camel.dataformat.yaml-snakeyaml.use-application-context-class-loader | Use ApplicationContextClassLoader as custom ClassLoader | true | Boolean |
SnakeYAML can load any class from YAML definition which may lead to security breach so by default, SnakeYAML DataForma restrict the object it can load to standard Java objects like List or Long. If you want to load custom POJOs you need to add theirs type to SnakeYAML DataFormat type filter list. If your source is trusted, you can set the property allowAnyType to true so SnakeYAML DataForma won’t perform any filter on the types.
380.3. Using YAML data format with the SnakeYAML library Copia collegamentoCollegamento copiato negli appunti!
Turn Object messages into yaml then send to MQSeries
from("activemq:My.Queue") .marshal().yaml() .to("mqseries:Another.Queue");
from("activemq:My.Queue") .marshal().yaml() .to("mqseries:Another.Queue");
Copy to Clipboard Copied! Toggle word wrap Toggle overflow from("activemq:My.Queue") .marshal().yaml(YAMLLibrary.SnakeYAML) .to("mqseries:Another.Queue");
from("activemq:My.Queue") .marshal().yaml(YAMLLibrary.SnakeYAML) .to("mqseries:Another.Queue");
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Restrict classes to be loaded from YAML
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
380.4. Using YAML in Spring DSL Copia collegamentoCollegamento copiato negli appunti!
When using Data Format in Spring DSL you need to declare the data formats first. This is done in the DataFormats XML tag.
And then you can refer to those ids in the route:
380.5. Dependencies for SnakeYAML Copia collegamentoCollegamento copiato negli appunti!
To use YAML in your camel routes you need to add the a dependency on camel-snakeyaml 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 the download page for the latest versions).
<dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-snakeyaml</artifactId> <version>${camel-version}</version> </dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-snakeyaml</artifactId>
<version>${camel-version}</version>
</dependency>