【问题标题】:Returning value from Apache Camel route to Spring Boot controller从 Apache Camel 路由返回值到 Spring Boot 控制器
【发布时间】:2020-07-01 21:15:44
【问题描述】:

我正在从 Spring Boot 控制器调用骆驼路线。骆驼路线调用返回字符串值的 REST 服务,我试图将该值从骆驼路线返回到控制器。下面是 Spring Boot 控制器:

@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 服务的返回值,但该字符串并未返回给控制器。谁能建议如何将值返回给 Spring Boot 控制器?

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

【问题讨论】:

    标签: spring-boot apache-camel


    【解决方案1】:

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

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

    • 不要登录
    • 启用流缓存
    • 将响应从 http 转换为字符串(非流式传输且可重新读取安全)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多