1

我有一个处理来自 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)
4

1 回答 1

6
  • <when>在每个元素中使用 MEL 表达式来测试Content-Type标题:#[message.inboundProperties['Content-Type'] == 'text/html']
  • 使用该<otherwise>元素充当defaulta 的语句switch:从那里抛出异常。
于 2013-05-22T18:31:31.833 回答