< All Topics
Print

שימוש במדיאטור foreach ושמירת נתונים בקובץ

<?xml version="1.0" encoding="UTF-8"?>
<api context="/getCandidates" name="getCandidates" xmlns="http://ws.apache.org/ns/synapse">
    <resource methods="POST">
        <inSequence>
            <payloadFactory media-type="json">
                <format>
                    {"from": "2023-03-01", "to": "2023-03-30"}          
                </format>
                <args/>
            </payloadFactory>
            <call>
                <endpoint>
                    <http method="post" uri-template="https://exampleservice.com/example">
                    </http>
                </endpoint>
            </call>
            <foreach expression="json-eval($)" id="DataRows">
                <sequence>
                    <log level="full"/>
                    <payloadFactory media-type="json">
                        <format>
  			  		  	 {
       						 "dateOpen": "$1",
       						 "candidateName": "$2",
       						 "candidateStatus": "$3",
    					 }
   					  </format>
                        <args>
                            <arg evaluator="json" expression="$.dateOpen"/>
                            <arg evaluator="json" expression="$.candidateName"/>
                            <arg evaluator="json" expression="$.candidateStatus"/>
                        </args>
                    </payloadFactory>
                </sequence>
            </foreach>
            <property name="transport.vfs.ReplyFileName" scope="transport" type="STRING" value="fileName.json"/>
            <property name="OUT_ONLY" scope="default" type="STRING" value="true"/>
            <call>
                <endpoint>
                    <address uri="vfs:file:///c:/wso2/exampleFolder/">
                        <suspendOnFailure>
                            <initialDuration>-1</initialDuration>
                            <progressionFactor>1</progressionFactor>
                        </suspendOnFailure>
                        <markForSuspension>
                            <retriesBeforeSuspension>0</retriesBeforeSuspension>
                        </markForSuspension>
                    </address>
                </endpoint>
            </call>
            <respond/>
        </inSequence>
        <outSequence/>
        <faultSequence/>
    </resource>
</api>

בדוגמה לעיל ניתן לראות שימוש במדיאטור foreach.
ה-API מקבל מערך של נתונים עם הרבה מאוד שדות ממקור חיצוני אודות מועמדים ב-jSON.
מטרת השימוש ב-foreach במקרה שלנו היא לרוץ על כל הנתונים עבור כל מועמד ולשמור רק את השדות שמעניינים אותנו (מקרה הזה dateOpen, candidateName, candidateStatus) ובסיום לשמור מערך ב-jSON עם השדות שצוינו לעיל בקובץ במחשב – לוקאלית.

תוכן עיניינים