How-to Guides

DataFormat DSL

qianmoQqianmoQ· 更新于 2026-09-22· 阅读 6 分钟· 0 次阅读

登录后可跨设备保存划线和私人笔记登录

Data Format DSL

The Data Format DSL is a builder API that allows using type safe construction of Camel Data Formats.

The Data Format DSL is exclusively available as part of the Java DSL.

The DSL can be accessed directly from the RouteBuilder thanks to the method dataFormat().

Using Data Format DSL

In the following example, a CsvDataFormat is created using the legacy approach where the data format is instantiated explicitly and configured using setters:

Java-only: creating a CSV data format using the legacy approach

public class MyRoutes extends RouteBuilder {
    @Override
    public void configure() {
        CsvDataFormat dataFormat = new CsvDataFormat(); (1)
        dataFormat.setDelimiter("|"); (2)
        from("direct:format")
            .setBody(constant(Map.of("foo", "abc", "bar", 123)))
            .marshal(dataFormat); (3)
    }
}
1Instantiate the expected data format
2Configure the data format according to the needs
3Affect the data format with the expected configuration

The previous code could be simplified using the utility methods available directly from the DataFormatClause corresponding to the type returned by the marshal() and unmarshal() methods:

Java-only: simplified marshalling using DataFormatClause utility method

public class MyRoutes extends RouteBuilder {
    @Override
    public void configure() {
        from("direct:format")
            .setBody(constant(Map.of("foo", "abc", "bar", 123)))
            .marshal()
            .csv(); (1)
    }
}
1Select the csv data format with the default delimiter

This approach is suitable for very basic configuration, but as there are only limited utility methods for each supported data format, for more complex configuration, we can quickly face situations where the utility method for our expected configuration doesn’t exist. In this situation, you can either use the legacy approach or the data format DSL like in the next code snippet:

Java-only: configuring CSV marshalling using the Data Format DSL builder

public class MyRoutes extends RouteBuilder {
    @Override
    public void configure() {
        from("direct:format")
            .setBody(constant(Map.of("foo", "abc", "bar", 123)))
            .marshal(
                dataFormat() (1)
                    .csv() (2)
                        .delimiter(",") (3)
                    .end() (4)
            );
    }
}
1Give access to all the supported data formats
2Select the csv data format
3Configure the data format according to the needs
4Build the data format with the expected configuration

评论

登录后参与评论

正在加载评论…