0

我正在使用 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 中获得这种格式

4

2 回答 2

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>
于 2014-01-24T01:02:31.877 回答
0

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

于 2013-11-28T15:26:31.153 回答