如何在MULE ESB中响应自定义JSON到HTTP请求

时间:2013-11-28 10:41:34

标签: mule soa mule-studio mule-el

我正在使用mule studio我连接到postgressql进行数据选择我做得很好我只是在中间发现。我正在发送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}]

但无法发送卷曲客户端我希望以此格式向客户发送回复

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

我如何在muleesb中获得此格式

2 个答案:

答案 0 :(得分:1)

撰写流程:[数据库] - &gt; [Object-to-JSON] - &gt; [设置有效负载]

像这样设置[设置有效载荷]的值

{"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>

答案 1 :(得分:0)

使用expression-transformer构建地图地图地图,将有效负载包装在{"ResponseJSON": {"Body":{"Datalist"中。将此地图序列化为JSON,您就完成了。