Search code examples
spring-bootapache-camelspring-camel

Propagate request param value in Camel SpringBoot project


Hello I have Spring Boot 2 project and I am using a camel for routes.

I have a camel rest endpoint and a Camel route:

 rest("/").produces("application/json")
.get("hello")
.param().name("url").type(RestParamType.query)
.dataType("String").endParam()
.to("direct:hello");
/////////////////////////////////////////////    
  System.out.println("starterd");
boolean startupRoute = true;
from("direct:hello").autoStartup(startupRoute)
    .tracing()
    .streamCaching()
    .process(exchange -> exchange.getIn()
        .setBody(exchange.getIn()
            .getBody()))
    .convertBodyTo(String.class)
    .marshal()
    .json(JsonLibrary.Jackson)
    .setHeader("Content-Type", constant("application/json"))
    .setHeader("Accept", constant("application/json"))
    .setHeader(Exchange.HTTP_METHOD, constant("GET"))
    .setHeader(Exchange.HTTP_URI).header("url")
    .log(LoggingLevel.INFO, "${body}")
    .removeHeader(Exchange.HTTP_PATH)
    .to("http4://url")
    .log(LoggingLevel.INFO, "This is my body: ${body}")
    .to("activemq://hello?exchangePattern=InOnly");
System.out.println("finished");

What I want to do is when I send a request like this:

http://localhost:8080/camel/hello/?url=http://localhost:8081/hi

The value of url to be set in the first .to() in the route:

.to("{url}?bridgeEndpoint=true")

I have tried with spring boot rest controller too but I still have problems with getting the value of the parameter in the .to(${url}

@GetMapping(value = "/finally")
  public String sendFromEndpointToActiveMq(@RequestParam(value = "url") String url) throws Exception {

  producerTemplate.sendBody("direct:hello", url);

return "done";

EDIT: I have edited the route


Solution

  • Try with this

    System.out.println("starterd");
    boolean startupRoute = true;
    from("direct:hello").autoStartup(startupRoute)
        .tracing()
        .streamCaching()
        .process(exchange -> exchange.getIn()
            .setBody(exchange.getIn()
                .getBody()))
        .convertBodyTo(String.class)
        .marshal()
        .json(JsonLibrary.Jackson)
        .setHeader("Content-Type", constant("application/json"))
        .setHeader("Accept", constant("application/json"))
        .setHeader(Exchange.HTTP_METHOD, constant("GET"))
        .setHeader(Exchange.HTTP_URI)
        .header("url")
        .log(LoggingLevel.INFO, "${body}")
        .removeHeader(Exchange.HTTP_PATH)
        .to("http4://url")
        .to("direct:hi");
    
    from("direct:hi").log(LoggingLevel.INFO, "This is my body: ${body}")
        .recipientList(simple("activemq://${header.activemq}"+"?exchangePattern=InOnly"));
    System.out.println("finished");