3

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

xmlParser
  • 1,903
  • 4
  • 19
  • 48

2 Answers2

3

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");
Bambus
  • 1,493
  • 1
  • 15
  • 32
1

You have to use the http component http://camel.apache.org/http4.html

You can override the HTTP endpoint URI by adding a header with the key, Exchange.HTTP_URI, on the message

.setHeader(Exchange.HTTP_URI).header("url")
.to("http4://dummy")
Aku Nour
  • 406
  • 4
  • 9
  • 1
    Hmm the thing that I want to do is to use url in the .to? .to(${url}) like this... And when I put it I always get error like No endpoint could be found and etc – xmlParser Mar 25 '19 at 08:22
  • 1
    if you add the 'url' query param to the Exchange.HTTP_URI header and then call .to with the http4 component it should call the endpoint in the 'url' header/query param. give it a try – Aku Nour Mar 25 '19 at 08:25
  • 1
    I am getting: No component found with scheme: http4 even though I added camel-http4 dependency... – xmlParser Mar 25 '19 at 08:34
  • 1
    I fixed the problem with the http4. Now when I send a request I received error with only dummy... What can i put instead of dummy? I have tried with ${url} but it didnt helped... – xmlParser Mar 25 '19 at 08:45
  • 1
    When i remove it I get: If you are forwarding/bridging http endpoints, then enable the bridgeEndpoint option on the endpoint: http4://dummy – xmlParser Mar 25 '19 at 09:01
  • 1
    Something that I found: If the camel-http4 endpoint is working in bridge mode, it will ignore the header Exchange.HTTP_URI. – xmlParser Mar 25 '19 at 09:23
  • yes thats why you should set the option to false(default). Can you post your route again – Aku Nour Mar 25 '19 at 09:43
  • What you mean by: Can you post your route again ? If you mean to edit the route, I have done that now.. – xmlParser Mar 25 '19 at 09:51