【问题标题】:How do I check the inbound header "content-type" response in mule?如何检查 mule 中的入站标头“内容类型”响应?
【发布时间】:2013-05-17 22:01:03
【问题描述】:

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

选择语句可以检查入站标头内容类型吗?是否可以进行默认检查的开关?有什么可以看的例子吗?

--- 更新---

我在下面尝试了 Davids 的建议:

        <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)

【问题讨论】:

  • 你用的是什么Mule版本?

标签: java rest content-type mule


【解决方案1】:
  • 在每个 &lt;when&gt; 元素中使用 MEL 表达式来测试 Content-Type 标头:#[message.inboundProperties['Content-Type'] == 'text/html']
  • 使用&lt;otherwise&gt; 元素充当switchdefault 语句:从那里抛出你的异常。

【讨论】:

  • 我尝试在本页示例中添加多个 when 语句,但最终出现错误。我使用了“==”和“包含”,但错误仍然存​​在。 mulesoft.org/documentation/display/current/…
  • 您在表达式末尾缺少关闭 ]
  • 我不确定为什么这些没有出现在上面的代码中。他们在我的项目中。我将更新上面的代码。即使添加了缺少的括号,我也有同样的错误。
  • 其实如果你把choice放在http:outbound-endpoint元素之后,你就不需要把它放在response块里了。
  • 使用 == 比较值时,请记住 Content-Type 也可能包含编码(例如 text/html; charset=utf-8
猜你喜欢
  • 2011-09-30
  • 2021-05-23
  • 2020-11-20
  • 2021-08-22
  • 2022-11-16
  • 2021-11-26
  • 1970-01-01
  • 1970-01-01
  • 2017-02-11
相关资源
最近更新 更多