我有一个流程来处理来自REST服务的响应:
<http:outbound-endpoint method="GET"
address="http://www.SomeAddress.com"
exchange-pattern="request-response">
...
<response>
<json:json-to-object-transformer/>
<custom-transformer ... />
</response>
</http:outbound-endpoint>
最近该服务已更新为返回html内容。我希望能够在响应中识别内容类型:
<http:outbound-endpoint method="GET"
address="http://www.SomeAddress.com"
exchange-pattern="request-response">
...
<response>
<choice>
<when content-type is html>
<custom-transformer-for-html ... />
</when>
<otherwise>
<json:json-to-object-transformer/>
<custom-transformer ... />
</otherwise>
</choice>
</response>
</http:outbound-endpoint>
更好的是,如果内容类型无法识别,那么简单地抛出异常会很好,类似于传统的switch语句:
switch (content-type) {
case (html) : ...
case (json): ...
default: throw exception
}
选择语句可以检查入站标题内容类型吗?交换机是否可以进行默认检查?我有什么例子可以看吗?
---更新---
我在下面尝试了戴维斯的建议:
<response>
<choice>
<when expression="#[message.inboundProperties['Content-Type'].contains('text/html')]">
<logger message="when number one invoked" level="WARN"/>
</when>
<when expression="#[message.inboundProperties['Content-Type'].contains('application/json')]">
<logger message="when number two invoked" level="WARN"/>
</when>
<otherwise>
<logger message="otherwise invoked" level="WARN"/>
</otherwise>
</choice>
</response>
但是现在我在编译时遇到以下错误:
cvc-complex-type.2.4.a: Invalid content was found starting with element 'choice'.
One of '{"http://www.mulesoft.org/schema/mule/core":abstract-transformer,
"http://www.mulesoft.org/schema/mule/core":abstract-filter,
"http://www.mulesoft.org/schema/mule/core":abstract-security-filter,
"http://www.mulesoft.org/schema/mule/core":abstract-intercepting-message-processor,
"http://www.mulesoft.org/schema/mule/core":abstract-observer-message-processor,
"http://www.mulesoft.org/schema/mule/core":processor,
"http://www.mulesoft.org/schema/mule/core":custom-processor}' is expected.
(org.xml.sax.SAXParseException)
org.apache.xerces.util.ErrorHandlerWrapper:-1 (null)
答案 0 :(得分:6)
<when>
元素中使用MEL表达式来测试Content-Type
标题:#[message.inboundProperties['Content-Type'] == 'text/html']
<otherwise>
元素充当default
的{{1}}语句:从那里抛出异常。