HoistField.java


Overview

The `HoistField` class is a simple, focused utility within the Apache Camel Kafka component's message transformation utilities. Its primary purpose is to **wrap (or "hoist") the existing message body inside a new JSON object under a specified field name**.

This transformation is commonly used when you need to nest the entire current message payload inside a new JSON structure, for example, to comply with a schema, prepare the message for downstream processing, or add contextual grouping around the existing data.

The class leverages Jackson's `ObjectMapper` and `JsonNode` for JSON processing and integrates with Apache Camel's `Exchange` abstraction, making it suitable for use as a processor or transformation step in Camel routes handling Kafka messages.


Class: HoistField

Package

org.apache.camel.component.kafka.transform

Description

`HoistField` provides a method `process` that takes a field name (from an exchange property) and the Camel `Exchange` object. It produces a new JSON object where the original message body is set as the value of the specified field, effectively nesting the original content one level deeper.


Method Summary

Method Signature

Description

`public JsonNode process(@ExchangeProperty("field") String field, Exchange ex) throws InvalidPayloadException`

Wraps the current message body inside a new JSON object under the given `field` name and returns it as a `JsonNode`.


Detailed Method Explanation

process

public JsonNode process(@ExchangeProperty("field") String field, Exchange ex) throws InvalidPayloadException
// Assuming this is called within a Camel route processor or bean
HoistField hoistField = new HoistField();

// Set the field name in the exchange property (usually done by Camel route or processor configuration)
exchange.setProperty("field", "payload");

// Process to hoist the body
JsonNode hoistedJson = hoistField.process(exchange.getProperty("field", String.class), exchange);

// Optionally set the new body on the message
exchange.getMessage().setBody(hoistedJson);
{
  "id": 123,
  "name": "Alice"
}
field = "user"
{
  "user": {
    "id": 123,
    "name": "Alice"
  }
}

Important Implementation Details


Interaction with Other System Components


Visual Diagram

classDiagram
    class HoistField {
        +JsonNode process(String field, Exchange ex) throws InvalidPayloadException
    }
    HoistField ..> ObjectMapper : uses
    HoistField ..> Exchange : accesses

Summary

The `HoistField` class encapsulates a straightforward but often-needed JSON transformation: wrapping an existing message body into a new JSON object under a specified field name. It provides flexibility by accepting the target field name dynamically from the Camel exchange properties and producing a Jackson `JsonNode` output ready for further processing or direct message body replacement.

This utility fits within the broader **Message Transformation Utilities** in the Apache Camel Kafka component, enabling clean and maintainable JSON payload restructuring in Kafka messaging workflows.


Appendix: Usage in Apache Camel Route (Example)

from("kafka:input-topic")
    .process(exchange -> {
        // Set the field name property dynamically (could be static or expression-based)
        exchange.setProperty("field", "wrappedPayload");
    })
    .process(new HoistField())
    .marshal().json() // Convert JsonNode to JSON string
    .to("kafka:output-topic");

This route snippet wraps incoming messages under `"wrappedPayload"` before forwarding them.


References