This is an automated email from the ASF dual-hosted git repository. jamesnetherton pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel-quarkus.git
commit e40ff0543db19d7f4324300e2f358a48f19579e9 Author: James Netherton <[email protected]> AuthorDate: Thu Jul 21 14:06:24 2022 +0100 Document ways of customizing ObjectMapper for JacksonDataFormat Fixes #3921 --- .../ROOT/pages/reference/extensions/jackson.adoc | 77 ++++++++++++++++++++++ extensions/jackson/runtime/src/main/doc/usage.adoc | 73 ++++++++++++++++++++ 2 files changed, 150 insertions(+) diff --git a/docs/modules/ROOT/pages/reference/extensions/jackson.adoc b/docs/modules/ROOT/pages/reference/extensions/jackson.adoc index d7b0eda701..c3a171043a 100644 --- a/docs/modules/ROOT/pages/reference/extensions/jackson.adoc +++ b/docs/modules/ROOT/pages/reference/extensions/jackson.adoc @@ -38,3 +38,80 @@ Or add the coordinates to your existing project: ---- Check the xref:user-guide/index.adoc[User guide] for more information about writing Camel Quarkus applications. + +== Usage + +=== Configuring the Jackson `ObjectMapper` + +There are a few ways of configuring the `ObjectMapper` that the `JacksonDataFormat` uses. These are outlined below. + +==== `ObjectMapper` created internally by `JacksonDataFormat` + +By default, `JacksonDataFormat` will create its own `ObjectMapper` and use the various configuration options on the `DataFormat` +to configure additional Jackson modules, pretty printing and other features. + +==== Custom `ObjectMapper` for `JacksonDataFormat` + +You can pass a custom `ObjectMapper` instance to `JacksonDataFormat` as follows. + +[source,java] +---- +ObjectMapper mapper = new ObjectMapper(); +JacksonDataFormat dataFormat = new JacksonDataFormat(); +dataFormat.setObjectMapper(mapper); +---- + +==== Using the Quarkus Jackson `ObjectMapper` with `JacksonDataFormat` + +The Quarkus Jackson extension exposes an `ObjectMapper` CDI bean which can be discovered by the `JacksonDataFormat`. + +[source,java] +---- +ObjectMapper mapper = new ObjectMapper(); +JacksonDataFormat dataFormat = new JacksonDataFormat(); +// Make JacksonDataFormat discover the Quarkus Jackson `ObjectMapper` from the Camel registry +dataFormat.setAutoDiscoverObjectMapper(true); +---- + +If you are using the JSON binding mode in the Camel REST DSL and want to use the Quarkus Jackson `ObjectMapper`, it can be achieved as follows. + +[source,java] +---- +@ApplicationScoped +public class Routes extends RouteBuilder() { + public void configure() { + restConfiguration().dataFormatProperty("autoDiscoverObjectMapper", "true"); + // REST definition follows... + } +} +---- + +You can perform customizations on the Quarkus `ObjectMapper` with a `ObjectMapperCustomizer`. + +[source,java] +---- +@Singleton +public class RegisterCustomModuleCustomizer implements ObjectMapperCustomizer { + public void customize(ObjectMapper mapper) { + mapper.registerModule(new CustomModule()); + } +} +---- + +It's also possible to `@Inject` the Quarkus `ObjectMapper` and pass it to the `JacksonDataFormat`. + +[source,java] +---- +@ApplicationScoped +public class Routes extends RouteBuilder() { + @Inject + ObjectMapper mapper; + + public void configure() { + JacksonDataFormat dataFormat = new JacksonDataFormat(); + dataFormat.setObjectMapper(mapper); + // Routes definition follows... + } +} +---- + diff --git a/extensions/jackson/runtime/src/main/doc/usage.adoc b/extensions/jackson/runtime/src/main/doc/usage.adoc new file mode 100644 index 0000000000..58b8061415 --- /dev/null +++ b/extensions/jackson/runtime/src/main/doc/usage.adoc @@ -0,0 +1,73 @@ +=== Configuring the Jackson `ObjectMapper` + +There are a few ways of configuring the `ObjectMapper` that the `JacksonDataFormat` uses. These are outlined below. + +==== `ObjectMapper` created internally by `JacksonDataFormat` + +By default, `JacksonDataFormat` will create its own `ObjectMapper` and use the various configuration options on the `DataFormat` +to configure additional Jackson modules, pretty printing and other features. + +==== Custom `ObjectMapper` for `JacksonDataFormat` + +You can pass a custom `ObjectMapper` instance to `JacksonDataFormat` as follows. + +[source,java] +---- +ObjectMapper mapper = new ObjectMapper(); +JacksonDataFormat dataFormat = new JacksonDataFormat(); +dataFormat.setObjectMapper(mapper); +---- + +==== Using the Quarkus Jackson `ObjectMapper` with `JacksonDataFormat` + +The Quarkus Jackson extension exposes an `ObjectMapper` CDI bean which can be discovered by the `JacksonDataFormat`. + +[source,java] +---- +ObjectMapper mapper = new ObjectMapper(); +JacksonDataFormat dataFormat = new JacksonDataFormat(); +// Make JacksonDataFormat discover the Quarkus Jackson `ObjectMapper` from the Camel registry +dataFormat.setAutoDiscoverObjectMapper(true); +---- + +If you are using the JSON binding mode in the Camel REST DSL and want to use the Quarkus Jackson `ObjectMapper`, it can be achieved as follows. + +[source,java] +---- +@ApplicationScoped +public class Routes extends RouteBuilder() { + public void configure() { + restConfiguration().dataFormatProperty("autoDiscoverObjectMapper", "true"); + // REST definition follows... + } +} +---- + +You can perform customizations on the Quarkus `ObjectMapper` with a `ObjectMapperCustomizer`. + +[source,java] +---- +@Singleton +public class RegisterCustomModuleCustomizer implements ObjectMapperCustomizer { + public void customize(ObjectMapper mapper) { + mapper.registerModule(new CustomModule()); + } +} +---- + +It's also possible to `@Inject` the Quarkus `ObjectMapper` and pass it to the `JacksonDataFormat`. + +[source,java] +---- +@ApplicationScoped +public class Routes extends RouteBuilder() { + @Inject + ObjectMapper mapper; + + public void configure() { + JacksonDataFormat dataFormat = new JacksonDataFormat(); + dataFormat.setObjectMapper(mapper); + // Routes definition follows... + } +} +----
