PowerfullDeveloper
PowerfullDeveloper

Reputation: 101

Pass body value to another processing step

My problem is, I have to pass exchange body value (which is set in previous route) into .to("rest:get ...") step. I'm trying something like in the code below, but class variable id is equal to null in the important step, even though I'm setting its value in a custom processor one step before. I've also tried using simple() function, which can evaluate into exchange body, but the problem is I don't have access to exchange in .to("rest:get ...") method, I can only access it inside processor.

How can I pass exchange body value into .to("rest:get ...") step?

public class SomeRoute extends RouteBuilder {

    @ConfigProperty(name = "rest.external.host")
    String host;

    @ConfigProperty(name = "rest.external.endpoint")
    String endpoint;

    String id;

    @Override
    public void configure() throws Exception {
        restConfiguration().host(host);

        from("direct:routeEntry")
                .log(LoggingLevel.INFO, log, body().toString())
                .setHeader(Exchange.HTTP_METHOD, constant("GET"))
                .removeHeader(Exchange.HTTP_PATH)
                .removeHeaders("Camel*")
                .process(exchange -> {
                    id = exchange.getIn().getBody().toString();
                })
                .to("rest:get:" + endpoint + "/" + id)
                .convertBodyTo(String.class);
       }
    }

Upvotes: 1

Views: 359

Answers (2)

jwwallin
jwwallin

Reputation: 118

To explain why the id is null more simply: the value of that class variable is only read at the time of route instantiation.

The reason for it only being read once is that the endpoint URI is constructed once during route building and every execution uses it after the route has been built and started.

To get the result you want you need to use a dynamic to, like @TachaDeChoco said.

In dynamic to you can use simple-syntax to construct the final URI during route execution.

Upvotes: 0

TacheDeChoco
TacheDeChoco

Reputation: 3913

Use dynamic to , aka "toD":

.toD("rest:get:" + endpoint + "/${body}")

Upvotes: 1

Related Questions