Jackson
JVM since0.3.0 Native since0.3.0
Marshal POJOs to JSON and back using Jackson
Maven coordinates
Or add the coordinates to your existing project:
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-jackson</artifactId>
</dependency>
Check the 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.
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
.
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.
@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
.
@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
.
@ApplicationScoped
public class Routes extends RouteBuilder() {
@Inject
ObjectMapper mapper;
public void configure() {
JacksonDataFormat dataFormat = new JacksonDataFormat();
dataFormat.setObjectMapper(mapper);
// Routes definition follows...
}
}