How to respond custom JSON to HTTP request in MULE ESB

主宰稳场 提交于 2019-12-25 02:42:59

问题


i am using mule studio i am connecting to postgressql for data selection i am doing well i just struck in middle .i am sending curl request to mule like this

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

i am getting response from database but unable to format the data as per client requirement. my config is like this

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

as per above config i am getting response from database but unable to format my log is like this

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

but unable to send a curl client i wish to send response in this format to client

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

How i get this format in muleesb


回答1:


Use an expression-transformer to build the a Map of Map of Map to wrap your payload in {"ResponseJSON": {"Body":{"Datalist". Serialize this map to JSON and you'll be done.




回答2:


Compose flow: [DATABASE] -> [Object-to-JSON] -> [Set Payload]

Set [Set Payload]'s value like this

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

Whole Flow:

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


来源:https://stackoverflow.com/questions/20263844/how-to-respond-custom-json-to-http-request-in-mule-esb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!