Convert Variable To

The ConvertVariableTo EIP allows you to convert a variable to a different type.

The Convert Variable To eip supports 0 options, which are listed below.

Name Description Default Type

note

The note for this node.

String

description

The description for this node.

String

disabled

Whether to disable this EIP from the route during build time. Once an EIP has been disabled then it cannot be enabled later at runtime.

false

Boolean

name

Name of variable to convert its value. The simple language can be used to define a dynamic evaluated variable name.

String

type

The java type to convert to.

String

toName

To use another variable to store the result. By default, the result is stored in the same variable.

String

mandatory

Whether the conversion is mandatory. If mandatory and conversion is not possible, a NoTypeConversionAvailableException is thrown. Setting this to false means null may be returned if conversion is not possible.

true

Boolean

charset

To use a specific charset when converting.

String

The type is a FQN class name (fully qualified), so for example java.lang.String, com.foo.MyBean etc. However, Camel has shorthand for common Java types, most noticeable String can be used instead of java.lang.String. You can also use byte[] for a byte array.

Exchange properties

The Convert Variable To eip has no exchange properties.

Example

For example, to convert the foo variable to String:

  • Java

  • XML

  • YAML

from("seda:foo")
  .convertVariableTo("foo", String.class)
  .log("The variable content: ${variable.foo}");
<route>
  <from uri="seda:foo"/>
  <convertVariableTo name="foo" type="String"/>
  <log message="The variable content: ${variable.foo}"/>
</route>
- route:
    from:
      uri: seda:foo
      steps:
        - convertVariableTo:
            name: foo
            type: String
        - log:
            message: "The variable content: ${variable.foo}"

Convert to another variable

By default, the converted value is replaced in the existing variable. However, you can tell Camel to store the result into another variable, such as shown below where the value is stored in the bar variable:

  • Java

  • XML

  • YAML

from("seda:foo")
  .convertVariableTo("foo", "bar", String.class)
  .log("The variable content: ${variable.bar}");
<route>
  <from uri="seda:foo"/>
  <convertVariableTo name="foo" toName="bar" type="String"/>
  <log message="The variable content: ${variable.bar}"/>
</route>
- route:
    from:
      uri: seda:foo
      steps:
        - convertVariableTo:
            name: foo
            toName: bar
            type: String
        - log:
            message: "The variable content: ${variable.bar}"

Dynamic variable name

The ConvertVariableTo supports using Simple language for dynamic variable name.

Suppose you have multiple variables:

  • region

  • emea

  • na

  • pacific

And that region points to either emea, na or pacific which has some order details. Then you can use dynamic variable to convert the header of choice. Now suppose that the region variable has value emea:

  • Java

  • XML

  • YAML

from("seda:foo")
  .convertVariableTo("${variable.region}", String.class)
  .log("Order from EMEA: ${variable.emea}");
<route>
  <from uri="seda:foo"/>
  <convertVariableTo name="${variable.region}" type="String"/>
  <log message="Order from EMEA: ${variable.emea}"/>
</route>
- route:
    from:
      uri: seda:foo
      steps:
        - convertVariableTo:
            name: ${variable.region}
            type: String
        - log:
            message: "Order from EMEA: ${variable.emea}"