【问题标题】:camel: Modify output before routing骆驼:在路由之前修改输出
【发布时间】:2013-03-31 15:06:26
【问题描述】:

在骆驼路由器中我有下面的路由。

from("jetty:http://localhost:9092?matchOnUriPrefix=true").
    to("http://server:9093/service1?bridgeEndpoint=true&throwExceptionOnFailure=false")
    .to("http://server:9094/service2?bridgeEndpoint=true&throwExceptionOnFailure=false")
    .to("log:output")

上述路由工作正常。

但我的要求是在发送到 service2 之前修改 service1 的输出。就像我得到 <x>abc</x>

我必须把它转换成

<y><x>abc</x></y>

我尝试使用处理器,但我将 service2 的 exchange.getOut() 设为 null,而实际上它返回了 xml。

如果可能的话,有人可以帮助我吗?如果问题不清楚,请告诉我。

【问题讨论】:

  • 你能显示你的处理器代码吗?
  • 嗨@user-soma,下面是我的处理器代码。 .process(new Processor() { @Override public void process(Exchange exchange) throws Exception { Message in = exchange.getOut(); exchange.getOut().setBody(convertIncomingRequestToOutgoingRequest(in.getBody(IncomingRequest.class))); }

标签: rest apache-camel


【解决方案1】:

我不确定您在路由中的何处添加此处理器。进入处理器的消息在交换机上的输入消息中可用。我看到您正试图从中提取消息。

当您将正文设置在 out 消息上时,它可以在下一个端点或处理器的交换上的消息中使用,因此必须选择交换上的正确消息。

以下路线应该有意义,否则将您的整个路线与处理器一起粘贴到您的问题中,成员可以看到问题所在:

from("jetty:http://localhost:9092?matchOnUriPrefix=true")
    .to("http://server:9093/service1?bridgeEndpoint=true&throwExceptionOnFailure=false").
        process(new Processor() {
        public void process(Exchange exchange) throws Exception {
            String body = exchange.getIn().getBody(String.class);
            exchange.getOut().setBody(modifyBody(body);
        }
        })
    .to("http://server:9094/service2?bridgeEndpoint=true&throwExceptionOnFailure=false")
    .to("log:output");

其中 modifyBody 将是一个自定义方法,它将执行所需的转换。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多