只有当表达式在 apache 骆驼选择中起作用时才首先?

问题描述 投票:0回答:3

我有这条路线检查身体是否以“01”或“02”开头,并根据它调用不同的 bean。问题是只有第一个有效。例如,如果我发送以“01”开头的消息 它工作正常,但如果我的消息以“02”开头,则执行其他部分,我得到一个空体的错误消息。

<route id="genericService">
        <from uri="servlet:///genericService"/>
        <choice>
            <when>
                <simple>${body} regex "^01.*$"</simple>
                <bean ref="cardFacade" method="getBalance" />
            </when>
            <when>
                <simple>${body} regex "^02.*$"</simple>
                <bean ref="depositFacade" method="getBalance" />
            </when>
            <otherwise>
                <transform>
                    <simple>error:  ${body}</simple>
                </transform>
            </otherwise>
        </choice>
        <marshal>
            <json />
        </marshal>
        <transform>
            <simple>${body}</simple>
        </transform>
    </route>
regex apache-camel
3个回答
4
投票

问题是 servlet 组件将主体作为只能读取一次的流提供。因此,您需要启用流缓存,或者将消息主体转换为非流类型,例如 String 或 byte[]。

您可以在这里找到更多详情

还请参阅本页的第一个框


1
投票

我在 Java DSL 中遇到了同样的问题(不是编译问题)。我要做的是为每个选择添加 .endchoice() 如下所示:

from(endPointTopic)
.errorHandler(deadLetterChannel)
.log("Message from Topic is ${body} & header string is ${header.Action}" )          
.choice()
    .when(header("Action").isEqualTo("POST"))
        .setHeader(Exchange.HTTP_METHOD, constant("POST"))
        .setHeader("Content-Type", constant("application/json"))
        .convertBodyTo(String.class)
        .to("log:like-to-see-all?level=INFO&showAll=true&multiline=true")
        .to(privateApi)
        .log("POST request for " + topicName)
        .endChoice()
    .when(header("Action").isEqualTo("PUT"))
        .setHeader(Exchange.HTTP_METHOD, constant("PUT"))
        .setHeader("Content-Type", constant("application/json"))
        .convertBodyTo(String.class)
        .to("log:like-to-see-all?level=INFO&showAll=true&multiline=true")
        .to(privateApi)
        .log("PUT request for " + topicName)
        .endChoice()
    .when(header("Action").isEqualTo("DELETE"))
        .setHeader(Exchange.HTTP_METHOD, constant("DELETE"))
        .setHeader("Content-Type", constant("application/json"))
        .convertBodyTo(String.class)
        .to("log:like-to-see-all?level=INFO&showAll=true&multiline=true")
        .to(privateApi)
        .log("DELET request for " + topicName)
        .endChoice()
    .otherwise()
        .setHeader(Exchange.HTTP_METHOD, constant("GET"))
        .setHeader("Content-Type", constant("application/json"))
        .convertBodyTo(String.class)
        .to("log:like-to-see-all?level=INFO&showAll=true&multiline=true")
        .to(privateApi)
        .log("Un-known HTTP action so posting to GET queue")
        .endChoice();           

0
投票

你需要添加 .choice() .end() 一些东西如下:

 .choice()
   .when(header("Action").isEqualTo("POST"))
   
   .endChoice()
.end()
.choice()
   .when(header("Action").isEqualTo("PUT"))
  
   .endChoice()
.end()
© www.soinside.com 2019 - 2024. All rights reserved.