Integration with Log

Flow script

Consider the flow script below.
Also, refer to the summary and the element description for better understanding.

- from:
    uri: "rest:post:/hello"
    steps:
    - log:
        message: "Received request with body: ${body}"
    - unmarshal:
        json:
          library: Jackson
    - setBody:
        simple: '{"message": "Hello, ${body[name]}!"}'
    - to:
        uri: "log:info"

Summary

  • Request reception: the flow starts with the reception of a POST request at the /hello endpoint.

  • Request logging: the body of the received request is logged with the message "Received request with body: ${body}".

  • Response transformation: the message body is transformed into a JSON object with a personalized message that includes the value of the name field from the request.

  • Response logging: the new message body is logged using the log:info endpoint.

Element description

  1. from

    Defines the source of the flow, i.e., where Camel should start processing messages.

    • uri

      Specifies the URI of the REST endpoint. In this case, we are defining an endpoint that accepts POST requests at /hello.

      - from:
          uri: "rest:post:/hello"
  2. steps

    Within the steps block, we define the various stages that Camel should execute when a message (or request) arrives at the REST endpoint. In this example, we have three actions to be executed by the components: log, setBody and to.

        steps:
        - log:
            message: "Received request with body: ${body}"
        - unmarshal:
            json:
              library: Jackson
        - setBody:
            simple: '{"message": "Hello, ${body[name]}!"}'
        - to:
            uri: "log:info"
  3. log

    This component logs a message.

    • message

      The message to be logged. ${body} is a Camel expression representing the current message body. In this case, we log "Received request with body: ${body}" to indicate that the request was received and show the request body.

      - log:
          message: "Received request with body: ${body}"
  4. unmarshal

    Converts (or deserializes) data from a specific format into Java objects. This means that it transforms data in a human- or machine-readable format (such as JSON, XML, CSV, etc.) into a data structure that can be easily manipulated within Camel.

    • json

      • library: Jackson

        In the example, the unmarshal component converts the request body from JSON to a Java object using the Jackson library.

        - unmarshal:
                json:
                  library: Jackson
  5. setBody

    This component sets the current message body. We are transforming the received message body into a new response body.

    • simple

      Defines a simple expression (Simple Expression Language - SEL) to the new message body. In this example, we create a JSON object with a message that includes the value of the name field from the received request body.

      - setBody:
          simple: '{"message": "Hello, ${body[name]}!"}'
  6. to

    This component sends the message to a specified endpoint.

    • uri

      The URI of the endpoint where the message will be sent. log:info is a special Camel endpoint that logs the message at the information level (INFO).

      - to:
          uri: "log:info"
Thanks for your feedback!
EDIT

Share your suggestions with us!
Click here and then [+ Submit idea]