【问题标题】:How to returned with custmise response to client using Mule ESB如何使用 Mule ESB 向客户端返回自定义响应
【发布时间】:2013-11-27 15:29:22
【问题描述】:

我是 mule esb 的新手,但我知道其他 ESB 模式 我的问题是我已经做了一个示例,它将数据插入 DB。它工作正常,但它没有给客户端任何响应,所以客户端没有从服务器得到任何东西 我的示例代码是

<mule xmlns:json="http://www.mulesoft.org/schema/mule/json"
    xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:jdbc-ee="http://www.mulesoft.org/schema/mule/ee/jdbc"
    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.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd
http://www.mulesoft.org/schema/mule/ee/jdbc http://www.mulesoft.org/schema/mule/ee/jdbc/current/mule-jdbc-ee.xsd
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/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd">

<jdbc-ee:postgresql-data-source name="PostgreSQL_Data_Source" user="youtilitydba" password="45782dfff1" 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="insertintoDBFlow1" doc:name="insertintoDBFlow1">
        <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" doc:name="HTTP" path="httpPost"/>
        <logger message="log about input message: #[payload]" level="INFO" doc:name="Logger"/>

                <json:json-to-object-transformer doc:name="JSON to Object" returnClass="java.util.Map"></json:json-to-object-transformer>

        <jdbc-ee:outbound-endpoint exchange-pattern="one-way" queryKey="INSERT_TOKEN" queryTimeout="-1" connector-ref="Database" doc:name="Database">
            <jdbc-ee:query key="INSERT_TOKEN" value="insert into users(FirstName,lastname) values(#[message.payload.name],#[message.payload.id]);"/>
        </jdbc-ee:outbound-endpoint>
    </flow>

</mule>

我正在使用像这样的示例 JSON 客户端调用它

curl -H "Content-Type: application/json" -d '{"name":"kk","id":"anil"}' http://localhost:8081/httpPost

当我运行这个 json 时,它没有给出任何响应,但我希望给出这样的响应 {"ResponseJSON":{"Body":{"Datalist":{"Data":"Successfully Rows inserted"}},"Status":"200"}}

我将如何格式化我使用的上述格式

【问题讨论】:

    标签: json mule mule-studio mule-el


    【解决方案1】:

    您需要添加一个 Echo 组件,将有效负载设置为您要查找的值,并添加一个带有 status="200"contentType="application/json" 的 HTTP 响应构建器。

    &lt;/jdbc-ee:outbound-endpoint&gt; 之后和&lt;/flow&gt; 之前添加以下行

    <response>
            <http:response-builder status="200" contentType="application/json" doc:name="HTTP Response Builder"/>
        </response>
        <response>
            <set-payload value="{&quot;Body&quot;:{&quot;Datalist&quot;:{&quot;Data&quot;:&quot;Successfully Rows inserted&quot;}},&quot;Status&quot;:&quot;200&quot;}}" doc:name="Set Payload"/>
        </response>
        <echo-component doc:name="Echo"/>
    

    干杯,

    -马可。

    【讨论】:

    • thanx a ton..我是新手,因为这会建议我选择数据如何为选择制作有效负载
    • 我们如何像 java sop 一样在 muleesb 中记录消息。我这样做是正确的,而不是
    • 它会起作用的。甚至 #[payload] 也可以在 mule 消息中打印有效负载。
    • 试试这个:
    猜你喜欢
    • 1970-01-01
    • 2019-02-16
    • 1970-01-01
    • 2016-10-10
    • 1970-01-01
    • 2013-12-14
    • 2020-09-25
    • 2018-02-12
    • 2020-08-17
    相关资源
    最近更新 更多