从 Apache Camel 路由返回值到 Spring 引导控制器

Returning value from Apache Camel route to Spring Boot controller

我正在从 Spring 引导控制器调用骆驼路线。骆驼路线调用一个 REST 服务,该服务 return 是一个字符串值,我正在尝试 return 从骆驼路线到控制器的那个值。下面是 Spring 引导控制器:

@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;
    }

}

下面是我的骆驼路线:

@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}");
    }

}

在路由中,日志正在打印来自 REST 服务的 return 值,但该字符串未 returned 到控制器。任何人都可以建议如何 return Spring 引导控制器的值吗?

我使用的Spring引导版本是2.2.5,Apache Camel版本是3.0.1。

查看此常见问题解答 https://camel.apache.org/manual/latest/faq/why-is-my-message-body-empty.html

来自 http 的响应是基于流的,因此只能读取一次,然后通过日志和 "empty" 读取它作为响应。所以要么

  • 不记录
  • 启用流缓存
  • 将响应从 http 转换为字符串(不是流式传输和可重读安全的)