Mule - 在运行时以 JSON 和 XML 形式返回..?

发布于 2024-12-15 23:23:35 字数 1087 浏览 0 评论 0原文

在Mule中,目前我正在使用自定义转换器“JavaObjectToJSON”(作为响应转换器)将POJO转换为JSON,即我的组件类返回一个Java对象(ListHashMap) 并且此转换器将其转换为 JSON 并返回结果。

我的代码如下:

<flow name="OfferingDetails">
    <http:inbound-endpoint address="http://localhost:1212/jcore/offering/details" 
transformer-refs="HttpParams" responseTransformer-refs="JavaObjectToJSON">  
    </http:inbound-endpoint>

    <component class="main.java.com.raman.jcore.OfferingDetails"/>
</flow>

现在假设我想以 XML 形式返回结果。为此,我可以使用另一个自定义转换器“JavaObjectToXML”,它可以以类似的方式执行相同的操作。


但我的疑问是如何动态做到这一点。就像我让客户选择如何获取数据一样。他可以点击这样的 URL

  • :“http://localhost:1212/jcore/offering/details/?response=json”
  • “http://localhost:1212/jcore/offering/details.json/”

  • “http:// /localhost:1212/jcore/offering/details/?response=xml"
  • "http://localhost:1212/jcore/offering/details.xml/

所以我能够在运行时更改变压器,并以相应的表示法返回结果。有没有办法做到这一点?

或者除了更改变压器之外,还有其他解决方法吗? ?

请帮助我。

In Mule, currently I am using a custom transformer "JavaObjectToJSON" (as a Response Transformer) to convert a POJO to JSON i.e. my component class returns a Java Object (List or HashMap) and this transformer converts it into JSON and returns the result.

My code is like :

<flow name="OfferingDetails">
    <http:inbound-endpoint address="http://localhost:1212/jcore/offering/details" 
transformer-refs="HttpParams" responseTransformer-refs="JavaObjectToJSON">  
    </http:inbound-endpoint>

    <component class="main.java.com.raman.jcore.OfferingDetails"/>
</flow>

Now Suppose I want to return the result in XML. For this i can use another custom transformer "JavaObjectToXML" that can do the same in similar manner.


But my query how can i do this dynamically. Like if i give choice to Client on how it wants the data. And he can hit the URL like

  • "http://localhost:1212/jcore/offering/details/?response=json"
  • "http://localhost:1212/jcore/offering/details.json/"

OR

  • "http://localhost:1212/jcore/offering/details/?response=xml"
  • "http://localhost:1212/jcore/offering/details.xml/

And so i would be able to change the transformer at runtime, and return the result in corresponding notation. Is there any way to do it..??

or apart from Changing Transformer, any other workaround ??

Please Help me. I am Stuck in this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

那一片橙海, 2024-12-22 23:23:35

使用选择路由消息处理器在响应阶段选择正确的变压器。

类似的东西:

<flow name="OfferingDetails">
  <http:inbound-endpoint address="http://localhost:1212/jcore/offering/details"
                         transformer-refs="HttpParams" />  

  <component class="main.java.com.raman.jcore.OfferingDetails"/>

  <response>
    <choice>
      <when expression="message.getInboundProperty('http.request.path').contains('json')" evaluator="groovy">
        <transformer ref="JavaObjectToJSON" />
      </when>
      <otherwise>
        <transformer ref="JavaObjectToXML" />
      </otherwise>
    </choice>
  <response>
</flow>

Use a choice routing message processor to select the right transformer in the response phase.

Something in the line of:

<flow name="OfferingDetails">
  <http:inbound-endpoint address="http://localhost:1212/jcore/offering/details"
                         transformer-refs="HttpParams" />  

  <component class="main.java.com.raman.jcore.OfferingDetails"/>

  <response>
    <choice>
      <when expression="message.getInboundProperty('http.request.path').contains('json')" evaluator="groovy">
        <transformer ref="JavaObjectToJSON" />
      </when>
      <otherwise>
        <transformer ref="JavaObjectToXML" />
      </otherwise>
    </choice>
  <response>
</flow>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文