0
votes

I am calling a camel route from a Spring Boot controller. The camel route calls a REST service which returns a string value and I am trying to return that value from the camel route to the controller. Below is the Spring Boot controller:

@RestController
@RequestMapping("/demo/client")
public class DemoClientController {

    @Autowired private ProducerTemplate template;

    @GetMapping("/sayHello")
    public String sayHello() throws Exception {
        String response = template.requestBody("direct:sayHelloGet", null, String.class);
        return response;
    }

}

And below is my camel route:

@Component
public class MyRoute extends RouteBuilder {

    @Override
    public void configure() throws Exception {
        from("direct:sayHelloGet")
            .log("Route reached")
            .setHeader(Exchange.HTTP_METHOD, simple("GET"))
            .to("http://localhost:8080/demo/sayHello")
            .log("${body}");
    }

}

In the route, the log is printing the return value from the REST service but that String is not returned to the controller. Can anyone please suggest how to return the value to the Spring Boot controller?

The Spring Boot version I am using is 2.2.5 and Apache Camel version is 3.0.1.

1

1 Answers

1
votes

See this FAQ https://camel.apache.org/manual/latest/faq/why-is-my-message-body-empty.html

The response from http is streaming based and therefore only readable once, and then its read via the log and "empty" as the response. So either

  • do not log
  • enable stream caching
  • convert the response from http to a string (not streaming and re-readable safe)