Only at first when does the expression work in apache camel pick?
I have this route that checks if the body starts with "01" or "02" and calls it beans. the problem is that only the first one works. for example if I post a message that starts with "01" it works fine, but if my message starts with "02" the other part gets executed and I get an error with an empty body.
<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>
+3
source to share
2 answers
The problem is that the servlet component provides the body as a stream that can only be read once. Thus, you either need to enable stream caching or convert the message body to a non stream type such as String or byte [].
You can find more information here
Also see 1st box on this page
+3
source to share
I had the same problem in Java DSL (not compilation problem). What I need to do is add .endchoice () for each selection like below:
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
source to share