Camel Quarkus

Jackson

JVM since0.3.0 Native since0.3.0

Marshal POJOs to JSON and back using Jackson.

What’s inside

Please refer to the above link for usage and configuration details.

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

Jackson 3

This extension provides the jackson data format of camel-jackson3, which is based on Jackson 3 (tools.jackson), like the Quarkus Jackson extension. The data format uses the defaults of Jackson 3, and your own ObjectMapper, modules and (de)serializers must use the Jackson 3 API. See the Jackson 3 section of the Camel 4.24 upgrade guide for the differences to Jackson 2.

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.

import tools.jackson.databind.ObjectMapper;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.jackson3.JacksonDataFormat;

public class Routes extends RouteBuilder {
    public void configure() {
        ObjectMapper mapper = new ObjectMapper();
        JacksonDataFormat dataFormat = new JacksonDataFormat();
        dataFormat.setObjectMapper(mapper);
        // Use the dataFormat instance in a route definition
        from("direct:my-direct").marshal(dataFormat);
    }
}

Using the Quarkus Jackson ObjectMapper with JacksonDataFormat

The Quarkus Jackson extension exposes an ObjectMapper CDI bean which can be discovered by the JacksonDataFormat.

import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.jackson3.JacksonDataFormat;

public class Routes extends RouteBuilder {
    public void configure() {
        JacksonDataFormat dataFormat = new JacksonDataFormat();
        // Make JacksonDataFormat discover the Quarkus Jackson `ObjectMapper` from the Camel registry
        dataFormat.setAutoDiscoverObjectMapper(true);
        // Use the dataFormat instance in a route definition
        from("direct:my-direct").marshal(dataFormat);
    }
}

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 by setting autoDiscoverObjectMapper=true.

In application.properties:

camel.rest.data-format-properties[autoDiscoverObjectMapper] = true

Or via the Java DSL:

import org.apache.camel.builder.RouteBuilder;

@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 JsonMapperBuilderCustomizer.

import io.quarkus.jackson.JsonMapperBuilderCustomizer;
import tools.jackson.databind.json.JsonMapper;

@Singleton
public class RegisterCustomModuleCustomizer implements JsonMapperBuilderCustomizer {
    public void customize(JsonMapper.Builder builder) {
        builder.addModule(new CustomModule());
    }
}

It’s also possible to @Inject the Quarkus ObjectMapper and pass it to the JacksonDataFormat.

import tools.jackson.databind.ObjectMapper;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.jackson3.JacksonDataFormat;

@ApplicationScoped
public class Routes extends RouteBuilder {
    @Inject
    ObjectMapper mapper;

    public void configure() {
        JacksonDataFormat dataFormat = new JacksonDataFormat();
        dataFormat.setObjectMapper(mapper);
        // Use the dataFormat instance in a route definition
        from("direct:my-direct").marshal(dataFormat);
    }
}