【问题标题】:How to respond custom JSON to HTTP request in MULE ESB如何在 MULE ESB 中响应自定义 JSON 到 HTTP 请求
【发布时间】:2013-12-14 08:33:32
【问题描述】:

我正在使用 mule studio 我正在连接到 postgressql 以进行数据选择我做得很好我刚刚击中了中间。我正在像这样向 mule 发送 curl 请求

 curl -H "Content-Type: application/json" -d '{"id":"1"}' http://localhost:8081/selectdb

我正在从数据库中获得响应,但无法根据客户要求格式化数据。 我的配置是这样的

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:cxf="http://www.mulesoft.org/schema/mule/cxf" xmlns:scripting="http://www.mulesoft.org/schema/mule/scripting" xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" xmlns:jdbc-ee="http://www.mulesoft.org/schema/mule/ee/jdbc" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns:mulexml="http://www.mulesoft.org/schema/mule/xml" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:spring="http://www.springframework.org/schema/beans" version="EE-3.4.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/xml http://www.mulesoft.org/schema/mule/xml/current/mule-xml.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/ee/jdbc http://www.mulesoft.org/schema/mule/ee/jdbc/current/mule-jdbc-ee.xsd
http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd
http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd
http://www.mulesoft.org/schema/mule/scripting http://www.mulesoft.org/schema/mule/scripting/current/mule-scripting.xsd
http://www.mulesoft.org/schema/mule/cxf http://www.mulesoft.org/schema/mule/cxf/current/mule-cxf.xsd">
    <jdbc-ee:postgresql-data-source name="PostgreSQL_Data_Source" user="youtilitydba" password="Youtility11" url="jdbc:postgresql://localhost:5432/sample" transactionIsolation="UNSPECIFIED" doc:name="PostgreSQL Data Source"/>
    <jdbc-ee:connector name="Database" dataSource-ref="PostgreSQL_Data_Source" validateConnections="true" queryTimeout="-1" pollingFrequency="0" doc:name="Database"/>
    <flow name="selectfromdbFlow1" doc:name="selectfromdbFlow1">
        <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" path="selectdb" doc:name="HTTP"/>
        <json:json-to-object-transformer returnClass="java.util.Map" doc:name="JSON to Object"/>

        <jdbc-ee:outbound-endpoint exchange-pattern="request-response" queryKey="SELECT" queryTimeout="-1" connector-ref="Database" doc:name="Database">
            <jdbc-ee:query key="SELECT" value="select  firstname,lastname,id  from users where id =#[message.payload.id]"/>
        </jdbc-ee:outbound-endpoint>
        <response>
            <http:response-builder status="200" contentType="application/json" doc:name="HTTP Response Builder"/>
        </response>
        <logger message="#[payload]" level="INFO" doc:name="Logger"/>
        <response>
            <set-payload value="#[payload]" doc:name="Set Payload"/>
        </response>

                <echo-component doc:name="Echo"/>
            </flow>
</mule>

按照上面的配置,我从数据库得到响应,但无法格式化 我的日志是这样的

org.mule.api.processor.LoggerMessageProcessor: [{lastname=sk, firstname=naryanan, id=1}]

但无法发送 curl 客户端我希望以这种格式向客户端发送响应

{"ResponseJSON": {"Body":{"Datalist": [{lastname=sk, firstname=naryanan, id=1}]},"Status":"200"}}

我如何在 muleesb 中获得这种格式

【问题讨论】:

  • 删除echo-component:没用。如果您需要记录,请使用logger

标签: mule soa mule-studio mule-el


【解决方案1】:

编写流程:[DATABASE] -> [Object-to-JSON] -> [Set Payload]

这样设置[Set Payload]的值

{"ResponseJSON": {"Body":{"Datalist": #[payload]},"Status":"200"}}

整个流程:

<flow name="mulemuleFlow1" doc:name="mulemuleFlow1">
    <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" doc:name="HTTP"/>
    <jdbc:outbound-endpoint exchange-pattern="request-response" queryKey="SELECT" queryTimeout="-1" connector-ref="JDBC1" doc:name="Database">
        <jdbc:query key="SELECT" value="SELECT * FROM something;"/>
    </jdbc:outbound-endpoint>
    <json:object-to-json-transformer doc:name="Object to JSON"/>
    <set-payload value="{&quot;ResponseJSON&quot;: {&quot;Body&quot;:{&quot;Datalist&quot;: #[payload]},&quot;Status&quot;:&quot;200&quot;}}" doc:name="Set Payload"/>
</flow>

【讨论】:

    【解决方案2】:

    使用 expression-transformer 构建 Map of Map 的 Map 以将您的有效负载包装在 {"ResponseJSON": {"Body":{"Datalist" 中。将此映射序列化为 JSON,然后就完成了。

    【讨论】:

    • ��srjava.util.ArrayListx����a�Isizexpw sr$org.mule.util.CaseInsensitiveHashMap����gE�xpw ?@lastnametfft firstnametfftidt5xxy 将这种格式提供给客户端
    • 您可能缺少object-to-json-transformer。由于您打算生成 JSON 对象,因此顶部对象应该是地图而不是列表。
    • 但我无法获得正确的结果集
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-07
    • 1970-01-01
    • 2018-05-28
    • 1970-01-01
    • 2015-10-18
    相关资源
    最近更新 更多