2

I have a REST service in my Mule flow :-

<flow name="restServiceFlow1" doc:name="restFlow1">
        <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8082" doc:name="HTTP"/>
        <jersey:resources doc:name="REST">
            <component class="com.test.services.schema.maindata.v1.Impl.MainDataImpl"/>
        </jersey:resources>
 </flow>

and I have another flow which sends JSON request from a file to that service flow and consume it :-

<flow name="restFlow2" doc:name="restFlow2">
 <file:inbound-endpoint path="E:\backup\test" responseTimeout="10000" doc:name="File" connector-ref="File_Global">
                        <file:filename-regex-filter pattern="aa.txt" caseSensitive="false"/>
 </file:inbound-endpoint>

        <json:json-to-object-transformer returnClass="java.util.HashMap" doc:name="JSON to Object"/>
        <http:outbound-endpoint exchange-pattern="request-response" contentType="application/json" method="GET" address="http://localhost:8082/getData/insert/?id=#[payload.insertDataRequest[0].id]&amp;name=#[payload.insertDataRequest[0].name]&amp;age=#[payload.insertDataRequest[0].age]&amp;designation=#[payload.insertDataRequest[0].designation]" doc:name="HTTP"/>
     </flow>

Now my JSON request is :-

{
    "insertDataRequest": [
        {
            "id": "6",
            "name": "ddddd",
            "age": "55",
            "designation": "WQQQQQ"
        },
        {
            "id": "64",
            "name": "mmmm",
            "age": "545",
            "designation": "TTTTTTTTTT"
        }
    ]
}

Now, the issue is whenever I place the JSON request as a file ... only the first Data is inserted .. that is only

{
                "id": "6",
                "name": "ddddd",
                "age": "55",
                "designation": "WQQQQQ"
 }

is getting inserted in DB ..enter image description here

.... .... Now I want all the Data to be inserted in Database ... How can I achieve it ?? ..Do I need any for each to get all the Data ??... Please Help...

Anirban Sen Chowdhary
  • 8,233
  • 6
  • 39
  • 81
  • I can't see your config or code for inserting into the db so can't really answer. Can you post it? Also why are you converting to a map before sending over http. – Ryan Carter Jul 13 '14 at 10:32
  • Hi Ryan .. Thanks for your response ... Pls find my updated question for inserting into DB .. I have used JDBCTemplate to insert data which is working fine in the service ... My only issue is inserting all the data at a time using JSON response .. for example if I use following response :-{"insertDataRequest": [{"id": "6","name": "ddddd","age": "55","designation": "WQQQQQ"} ]} .. then it's work fine and data getting inserted .. – Anirban Sen Chowdhary Jul 13 '14 at 10:47
  • but if I use following JSON request :-{"insertDataRequest": [{"id": "6","name": "ddddd","age": "55","designation": "WQQQQQ"},{"id": "64", "name": "mmmm","age": "545","designation": "TTTTTTTTTT"}]} ... only first row getting inserted .. how to insert all the rows from the JSON request... ??? – Anirban Sen Chowdhary Jul 13 '14 at 10:48
  • I am using restFlow2 Flow in Mule config to pass the JSON request from file to consume the service – Anirban Sen Chowdhary Jul 13 '14 at 10:55

1 Answers1

4

Use a for each block to iterate on the insertDataRequest entries:

<flow name="restFlow2">
  <file:inbound-endpoint path="E:\backup\test" responseTimeout="10000" connector-ref="File_Global">
    <file:filename-regex-filter pattern="aa.txt" caseSensitive="false"/>
  </file:inbound-endpoint>

  <json:json-to-object-transformer returnClass="java.util.HashMap"/>

  <foreach collection="#[payload.insertDataRequest]">
    <http:outbound-endpoint exchange-pattern="request-response"
          contentType="application/json" method="GET"
          address="http://localhost:8082/getData/insert/?id=#[payload.id]&amp;name=#[payload.name]&amp;age=#[payload.age]&amp;designation=#[payload.designation]"/>
  </foreach>
</flow>

Note that the fact you're performing insertions with GET instead of a POST is not respectful of the REST principles...

David Dossot
  • 33,403
  • 4
  • 38
  • 72
  • Superb ... This is what I was expecting .. Thanks David ... It's working ... One small question .. How can I get the root node name ? for example here I have insertDataRequest as root node in JSON request .. now I may have different root node like updateDataRequest .. now how to get the root node name in the flow ?? I am asking because I need to route to different http outbound endpoint based on node name .. – Anirban Sen Chowdhary Jul 13 '14 at 15:29
  • 2
    After the `json-to-object-transformer`, you can get the "root node name" (actually the first field name of the top JSON object) with: `#[message.payload.keySet().iterator().next()]` – David Dossot Jul 13 '14 at 16:05