Question

I am new for the mule esb but i know other ESB patterns my issue is i have done a sample which will insert a data into DB.its working fine but its not giving any response to client so client is getting nothing from server my sample code is

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

and i am calling to this using sample JSON client like this

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

when i am running this json its giving nothing response but i wish to give response like this {"ResponseJSON":{"Body":{"Datalist":{"Data":"Successfully Rows inserted"}},"Status":"200"}}

how would i format this above format i used http response builder but there is nothing to do like this..

Était-ce utile?

La solution

you need to add an Echo component, set the payload to the value you are looking for and add a HTTP Response Builder with status="200" and contentType="application/json".

Add the following lines after </jdbc-ee:outbound-endpoint> and before </flow>

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

Cheers,

-Marco.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top