Apache Camel Rest send response for post request

7.9k views Asked by At

I have a camel endpoint where another application sends a post request with some data (Maybe with some other route)

I want to process this data and return something back to the application with the response of the POST request.

This is how my Camel Context looks at the moment:

 <camelContext xmlns="http://camel.apache.org/schema/blueprint">

    <restConfiguration component="restlet" bindingMode="json" port="8989" enableCORS="true"/>

    <rest path="/finData">
      <description>User rest service</description>
      <post>
        <to uri="direct:update"/>
      </post>
    </rest>

    <route id="sendFinData">
      <from uri="direct:update"/>
      <log message="Got some data:  ${body}"/>
      <to uri="aclient://otherClient"/>
    </route>

  </camelContext>

How can I send some answer back from the route sendFinData through the response of the post request?

2

There are 2 answers

2
KinGin On BEST ANSWER

The response the post request to your route receives is whatever is in your ${body} at the end of the route.

So in your route's end the ${body} contains whatever is the response from

<to uri="aclient://otherClient"/>

I don't use the Camel XML but in Java you would do:

    rest("/finData")
        .get()
        .route()
        .to("direct:sendFindData")
        .end();

    from("direct:sendFindData")
        .to("aclient://otherClient")
        .process(exchange -> exchange.getIn().setBody("Hello world"))
        .setBody(simple("GoodBye world")) // same thing as line above
        .end();

If the data you want to pass back to the requester is not the response of last API call in your route you need to save it somewhere temporarily (exchange.properties) and set it back to body later, or aggregate the responses so that the original data does not get overwritten. The route should produce data that the consumer expects. For normal rest requests this should be String type (like "GoodBye world"). If you want to return JSON for example be sure that the response body is JSON string at the end of the route.

Sorry that I'm not able to help with the XML but hope this is of some help to you.

0
Prathap M Belli On

In case you need to send back a response as an acknowledgment to the post request with custom data then use transform which changes ${body} as you want

<route id="sendFinData">
      <from uri="direct:update"/>
<log message="Got some data:  ${body}"/>
<transform>
            <simple>
                I got some data     
            </simple>
    </transform>
    </route>

above will send back modified response as ack to request

If you want to respond back and also keep original data to forward to other route or store then use multicast with transform

<route id="sendFinData">
          <from uri="direct:update"/>
    <log message="Got some data:  ${body}"/>
<multicast>
<to uri="aclient://otherClient"/>
    <transform>
                <simple>
                    I got some data     
                </simple>
        </transform>
</multicast>
        </route>

above will send a response as ack to request and also forwards original data to otherClient (uri="aclient://otherClient")

or if you want to send the modified body to uri="aclient://otherClient" then send it after transform.