InsertField.java


Overview

The `InsertField` class is a JSON message transformation processor designed for use within Apache Camel routes, specifically in Kafka integration pipelines. Its primary purpose is to insert a new field with a specified name and value into a JSON object payload or append a value to a JSON array payload carried in the message body of a Camel `Exchange`.

This processor supports dynamic resolution of the inserted value using Camel's Simple language expressions, enabling flexible and context-aware modifications of the JSON message content at runtime.


Class: InsertField

public class InsertField implements Processor

Purpose

`InsertField` implements the Apache Camel `Processor` interface to manipulate the JSON payload of a message by inserting a new field (for JSON objects) or appending a value (for JSON arrays). This class is typically used in Camel routes to enrich or extend JSON messages before further processing or sending to Kafka topics.

Fields

Field

Type

Description

`field`

`String`

The name of the new field to insert when the JSON body is an object. Ignored if the body is an array.

`value`

`String`

The value to insert or append. Can be a static string or a Camel Simple language expression for dynamic evaluation.

Constructors

InsertField()

InsertField(String field, String value)

InsertField insertFieldProcessor = new InsertField("status", "processed");

Methods

void process(Exchange ex) throws InvalidPayloadException

InsertField processor = new InsertField("newField", "${header.someHeader}");
processor.process(exchange);

void setField(String field)

insertFieldProcessor.setField("newField");

void setValue(String value)

insertFieldProcessor.setValue("${header.userId}");

Important Implementation Details


Interaction with Other Components


Usage Example in a Camel Route

from("kafka:input-topic")
    .process(new InsertField("processedBy", "${header.CamelId}"))
    .to("kafka:output-topic");

This route reads messages from `input-topic`, inserts a field `"processedBy"` with the current Camel context ID, and sends the enriched message to `output-topic`.


Mermaid Class Diagram for InsertField.java

classDiagram
    class InsertField {
        -String field
        -String value
        +InsertField()
        +InsertField(field: String, value: String)
        +void process(ex: Exchange) throws InvalidPayloadException
        +void setField(field: String)
        +void setValue(value: String)
    }
    InsertField ..|> Processor

Summary

The `InsertField` class is a lightweight yet powerful Camel `Processor` that enriches JSON message payloads by inserting new fields or appending values. Its support for Camel Simple language expressions enables dynamic and context-sensitive enhancements to Kafka messages, making it an essential building block in the **Message Transformation Utilities** suite for JSON content manipulation.


*End of InsertField.java Documentation*