【问题标题】:camel http component is not set the response in the exchange body骆驼http组件未在交换体中设置响应
【发布时间】:2020-08-30 13:08:48
【问题描述】:

我正在使用 Camel 版本 3.2.0 和 Spring Boot 版本 2.2.6.RELEASE 。

我正在使用 camel-http 组件来使用一个以 JSON 格式返回产品列表的休息服务。
我正在尝试存储在 csv 文件中。因为在out里没有设置http响应,所以body是空的。

我正在使用以下路线。

<route id="getProducts">
    <from uri="quartz://groupName/timerName?cron=0 0/2 * 1/1 * ? *" />
    <setHeader name="CamelHttpMethod">
      <constant>GET</constant>
    </setHeader>
    <to uri="http://localhost:8080/product"></to>
    <log message=" before processor \n Body ${body} \n headers ${headers}"/>
    <!-- <process ref="processor" /> -->
    <log message=" before convert \n Body ${body} \n headers ${headers}"/>
    <marshal>
        <bindy type="Csv"  classType="com.basf.vo.Product"   />
    </marshal>
    <log message=" after convert \n Body ${body}"/>
    <to uri="file:\balachandar\?fileName=abc.csv"/>
    <log message="Products Received \n ============================= \n"/>
</route>


<log message=" before processor \n Body ${body} \n headers ${headers}"/> => It is printing Body {"productId":1,"name":"Pink Ralph Lauren Polo Shirt","category":"Dress"} in the console

<log message=" before convert \n Body ${body} \n headers ${headers}"/>  => It is not printing the body. Body is empty.

I used custom processor to know the body.

public void process(Exchange exchange) throws Exception {
    String msg = exchange.getIn().getBody(String.class);
    String msg1 = exchange.getMessage(String.class);
    System.out.println("Rest Response is:->" + msg);   //  Rest Response is:->
    System.out.println("Rest Response is:->" + msg1);  //  Rest Response is:->null

}

它是第一次打印正文,但不是后续时间通过处理器或日志组件。
我不确定为什么没有在正文中设置响应。请帮忙。

【问题讨论】:

    标签: spring-boot apache-camel spring-camel


    【解决方案1】:

    响应类型是流。当您第一次记录响应时,您使用该流并且数据已经消失。在对消息正文执行任何其他操作之前,您可以启用 stream caching 或将流转换为字符串 (&lt;convertBodyTo type="java.lang.String"/&gt;)。

    【讨论】:

    • localhost:8080/product"></to> 它正在工作。
    【解决方案2】:

    使用 exchange.getOut().getBody(String.class) 获取响应正文。

    请参考链接: https://camel.apache.org/components/latest/http-component.html#_message_body

    【讨论】:

    • exchange.getOut().getBody(String.class) 在最新版本中已弃用。由 exchange.getMessage(String.class); 代替如前所述,它返回 null 值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-28
    • 2017-03-10
    • 2023-04-05
    • 1970-01-01
    • 2017-01-03
    • 1970-01-01
    • 2020-02-20
    相关资源
    最近更新 更多