PayloadFactory Mediator 使用

WSO2   2025-01-11 16:37   143   0  

一、返回 JSON

1、开启参数

<PRODUCT_HOME>/repository/conf/axis2/axis2.xml
<!--JSON Message Formatters-->
<messageFormatter class="org.apache.synapse.commons.json.JsonStreamFormatter" contentType="application/json"/>

image.png

<!--JSON Message Builders-->
<messageBuilder class="org.apache.synapse.commons.json.JsonStreamBuilder" contentType="application/json"/>

image.png

2、使用 PayloadFactory 设置返回的报文

①、Class Mediator 类里设置属性

package com;

import org.apache.synapse.MessageContext;
import org.apache.synapse.mediators.AbstractMediator;

public class MyTest extends AbstractMediator{

    @Override
    public boolean mediate(MessageContext context) {
        // TODO 自动生成的方法存根
        System.out.println("接收到的数据:" + (String) context.getProperty("mdmdata"));
        String jsonArray = (String) context.getProperty("datainfo");
        System.out.println("datainfo: " + jsonArray);
        String puuid = (String) context.getProperty("puuid");
        System.out.println("puuid: " + puuid);

        //在上下文设置属性
        context.setProperty("args", jsonArray);
        context.setProperty("args2", puuid);
        return true;
    }
}

②、REST API 获取请求报文

format 里面设置返回报文的 JSON 格式
获取请求里的报文(使用 JSONPath ):$.A.B.C.D

<?xml version="1.0" encoding="UTF-8"?>
<api context="/test2" name="test" xmlns="http://ws.apache.org/ns/synapse">
    <resource methods="POST GET">
        <inSequence>
            <property expression="json-eval($.)" name="mdmdata" scope="default" type="STRING"/>
            <property expression="json-eval($.ESB.DATA.DATAINFOS.DATAINFO)" name="datainfo" scope="default" type="STRING"/>
            <property expression="json-eval($.ESB.DATA.DATAINFOS.PUUID)" name="puuid" scope="default" type="STRING"/>
            <class description="" name="com.MyTest"/>
            <payloadFactory media-type="json">
                <format>{
                "esb": {
                    "args1" : $1,
                    "args2" : "$2",
                  "args3" : "$3",
                    }
                }</format>
                <args>
                    <!--第一个参数表示$1,第二个是$2,以此类推-->
                    <!--这里的表达式获取的是这个请求里的报文,不是Class Mediator返回的属性-->
                    <arg evaluator="json" expression="$.ESB.DATA.DATAINFOS.DATAINFO"/>
                    <!--这里的表达式获取的是这个请求里的报文,不是Class Mediator返回的属性-->
                    <arg evaluator="json" expression="$.ESB.DATA.DATAINFOS.PUUID"/>
                    <!--通过xml获取上下文的属性-->
                    <arg evaluator="xml" expression="get-property('args2')"/>
                </args>
            </payloadFactory>
            <respond description=""/>
        </inSequence>
        <outSequence/>
        <faultSequence/>
    </resource>
</api>

②、REST API 获取上下文的属性

format 里面设置返回报文的 JSON 格式
获取上下文里的属性使用的表达式为:$ctx:变量名

<?xml version="1.0" encoding="UTF-8"?>
<api context="/test2" name="test" xmlns="http://ws.apache.org/ns/synapse">
    <resource methods="POST GET">
        <inSequence>
            <property expression="json-eval($.)" name="mdmdata" scope="default" type="STRING"/>
            <property expression="json-eval($.ESB.DATA.DATAINFOS.DATAINFO)" name="datainfo" scope="default" type="STRING"/>
            <property expression="json-eval($.ESB.DATA.DATAINFOS.PUUID)" name="puuid" scope="default" type="STRING"/>
            <class description="" name="com.MyTest"/>
            <payloadFactory media-type="json">
                <!--这里是根据属性名获取上下文的属性,不需要配置args-->
                <format>{
                "esb": {
                    "args1" : $ctx:args,
                    "args2" : "$ctx:args2"
                    }
                }</format>
            </payloadFactory>
            <respond description=""/>
        </inSequence>
        <outSequence/>
        <faultSequence/>
    </resource>
</api>

二、返回 XML

1、使用 PayloadFactory 设置返回的报文

①、Class Mediator 类里设置属性

package com;

import org.apache.synapse.MessageContext;
import org.apache.synapse.mediators.AbstractMediator;

public class MyTest extends AbstractMediator{

    @Override
    public boolean mediate(MessageContext context) {
        // TODO 自动生成的方法存根
        System.out.println("接收到的数据:" + (String) context.getProperty("mdmdata"));
        String jsonArray = (String) context.getProperty("datainfo");
        System.out.println("datainfo: " + jsonArray);
        String puuid = (String) context.getProperty("puuid");
        System.out.println("puuid: " + puuid);

        //在上下文设置属性
        context.setProperty("args", jsonArray);
        context.setProperty("args2", puuid);
        return true;
    }
}

②、REST API 获取报文

format 里面设置返回报文的 XML 格式
获取请求里的报文(使用 Path )://A//B//C/D

<?xml version="1.0" encoding="UTF-8"?>
<api context="/test2" name="test" xmlns="http://ws.apache.org/ns/synapse">
    <resource methods="POST GET">
        <inSequence>
        <property expression="//ESB" name="mdmdata" scope="default" type="STRING"/>
            <property expression="//ESB//DATA//DATAINFOS//DATAINFO" name="datainfo" scope="default" type="STRING"/>
            <property expression="//ESB//DATA//DATAINFOS//PUUID" name="puuid" scope="default" type="STRING"/>
            <class description="" name="com.MyTest"/>
            <payloadFactory media-type="xml">
                <format>
                      <!-- 必须设置xmlns="",否则会使用默认的命名空间  -->
                    <esb xmlns="">
                        <args1>$1</args1>
                        <args2>$2</args2>
                        <args3>$3</args3>
                    </esb>
                </format>
                <args>
                      <!--这里的表达式获取的是这个请求里的报文,不是Class Mediator返回的属性-->
                    <arg evaluator="xml" expression="//ESB//DATA//DATAINFOS//DATAINFO"/>
                    <!--这里的表达式获取的是这个请求里的报文,不是Class Mediator返回的属性-->
                    <arg evaluator="xml" expression="//ESB//DATA//DATAINFOS//PUUID"/>
                    <!--通过xml获取上下文的属性-->
                    <arg evaluator="xml" expression="get-property('args2')"/>
                </args>
            </payloadFactory>
            <respond description=""/>
        </inSequence>
        <outSequence/>
        <faultSequence/>
    </resource>
</api>

②、REST API 获取上下文的属性

format 里面设置返回报文的 XML 格式
获取上下文里的属性使用的表达式为:$ctx:变量名

<?xml version="1.0" encoding="UTF-8"?>
<api context="/test2" name="test" xmlns="http://ws.apache.org/ns/synapse">
    <resource methods="POST GET">
        <inSequence>
        <property expression="//ESB" name="mdmdata" scope="default" type="STRING"/>
            <property expression="//ESB//DATA//DATAINFOS//DATAINFO" name="datainfo" scope="default" type="STRING"/>
            <property expression="//ESB//DATA//DATAINFOS//PUUID" name="puuid" scope="default" type="STRING"/>
            <class description="" name="com.MyTest"/>
            <payloadFactory media-type="xml">
                <format>
                    <!--这里是根据属性名获取上下文的属性-->
                      <!-- 必须设置xmlns="",否则会使用默认的命名空间  -->
                    <esb xmlns="">
                        <args1>$ctx:args</args1>
                        <args2>$ctx:args2</args2>
                    </esb>
                </format>
            </payloadFactory>
            <respond description=""/>
        </inSequence>
        <outSequence/>
        <faultSequence/>
    </resource>
</api>

三、测试报文

{
  "ESB": {
    "DATA": {
      "DATAINFOS": {
        "PUUID": "0XTSI9002B91F0129511374CM074R19U",
        "DATAINFO": [
          {
            "Abbreviation": "深圳润园四季餐饮有限公司",
            "CityCode": "440300",
            "CooperationStatus": "合作中",
            "TpmMainCategory": "辅食",
            "Address": "天安社区车公庙泰然七路1号博今商务广场三层03号商铺",
            "Customer": "深圳润园四季餐饮有限公司cesAA",
            "TpmKeyCustomer": "",
            "SendSystemName": "ERP,TPM,SFA",
            "SfaId": "8941778068727647925",
            "TaxPayerType": "一般纳税人",
            "PaymentTerms": "EOM45",
            "IsKeyCustomer": "否",
            "BusinessCorporation": "黄日高--测试&φ",
            "InvoiceBankAccount": "381626368",
            "InvoiceMobile": "36176767",
            "MarketingDepartment": "PRC Offline Sales",
            "InvoiceBankName": "测试20221014",
            "Credit": "30",
            "CertificateNumber": "914403005571835420",
            "InvoiceSubBankName": "测试20221014",
            "CountryCode": "CN",
            "SFASalesPath": "全部,PRC Offline South,粤东,深圳市",
            "MarketingType": "母婴-单店",
            "ChargePerson": "Andy Liu(刘平)",
            "InvoiceTaxNumber": "636772617",
            "CATEGORYCODE": "00",
            "MULTICODE": {
              "VALUELIST": [
                {
                  "PaymentAccountCode": "30000866",
                  "SPECIALITYCODE": "PAYMENTINFO",
                  "LISTCODE": "5I6FT34K4U5F7P0E23NT768513Y1661U"
                },
                {
                  "BussinessUnit": "BU_SFPRCSH",
                  "SPECIALITYCODE": "FINANCEINFO",
                  "LISTCODE": "43359CW4X0NMA89R88H46W66888SB2B8"
                },
                {
                  "ContactMobile": "17365452345",
                  "IsPrimaryContact": "是",
                  "SfaContactId": "9173566440704169777",
                  "SPECIALITYCODE": "CONTACTINFO",
                  "LISTCODE": "VE244A9Y3FNUWN7K747KFU6H6L84RHI5",
                  "ContactEMail": "22133133@qq.com",
                  "ContactName": "李迪"
                },
                {
                  "ContactMobile": "17365459878",
                  "IsPrimaryContact": "否",
                  "SfaContactId": "6788874131617504418",
                  "SPECIALITYCODE": "CONTACTINFO",
                  "LISTCODE": "6WQB8MA09Z4776I430SJPC2TU6K93UU4",
                  "ContactEMail": "2233133@qq.com",
                  "ContactName": "吴繁荣"
                },
                {
                  "SiteContactName": "刘三",
                  "SiteAddress": "测试77777",
                  "SfaSiteId": "8480089929551622116",
                  "SPECIALITYCODE": "SITEINFO",
                  "SiteCityCode": "110000",
                  "LISTCODE": "WAHVUTQXQP7S0GQ36Z562W0S9OZ3QYEG",
                  "SiteContactMobile": "21878128337"
                },
                {
                  "SiteContactName": "刘飒",
                  "SiteAddress": "测试00333003",
                  "SfaSiteId": "7037924554341270073",
                  "SPECIALITYCODE": "SITEINFO",
                  "SiteCityCode": "330300",
                  "LISTCODE": "8Y2PT3L762Y3IRBP9Y3FSHX90320997H",
                  "SiteContactMobile": "15876542345"
                }
              ]
            },
            "FILES": [
              {
                "STOREPATH": "https://image.waiqin365.com/6055786081413437802/cm/202210/ea56e72f4c2249ad8dc732517b36a634.jpg",
                "FILETYPE": "image/jpg",
                "FILENAME": "营业执照.jpg"
              },
              {
                "STOREPATH": "https://image.waiqin365.com/6055786081413437802/cm/202301/59d94e4583cf42d2aa321694c8f5f483.jpg",
                "FILETYPE": "image/jpg",
                "FILENAME": "开票资料(图片).jpg"
              }
            ],
            "CustomerType": "真实客户",
            "InvoiceType": "增值税专用发票",
            "TpmChannelType": "Offline-GT",
            "CODE": "10001267",
            "InvoiceAddress": "测试20221014",
            "TaxClassificationCode": "CN VAT OUT 13",
            "Country": "中国",
            "FromDate": "2022-10-14",
            "SourceSystem": "SFA",
            "InvoiceName": "测试20221014"
          }
        ]
      }
    }
  }
}
<ESB>
  <DATA>
    <DATAINFOS>
      <PUUID>0XTSI9002B91F0129511374CM074R19U</PUUID>
      <DATAINFO>
        <Abbreviation>深圳润园四季餐饮有限公司</Abbreviation>
        <CityCode>440300</CityCode>
        <CooperationStatus>合作中</CooperationStatus>
        <TpmMainCategory>辅食</TpmMainCategory>
        <Address>天安社区车公庙泰然七路1号博今商务广场三层03号商铺</Address>
        <Customer>深圳润园四季餐饮有限公司cesAA</Customer>
        <TpmKeyCustomer></TpmKeyCustomer>
        <SendSystemName>ERP,TPM,SFA</SendSystemName>
        <SfaId>8941778068727647925</SfaId>
        <TaxPayerType>一般纳税人</TaxPayerType>
        <PaymentTerms>EOM45</PaymentTerms>
        <IsKeyCustomer>否</IsKeyCustomer>
        <BusinessCorporation>黄日高--测试&amp;φ</BusinessCorporation>
        <InvoiceBankAccount>381626368</InvoiceBankAccount>
        <InvoiceMobile>36176767</InvoiceMobile>
        <MarketingDepartment>PRC Offline Sales</MarketingDepartment>
        <InvoiceBankName>测试20221014</InvoiceBankName>
        <Credit>30</Credit>
        <CertificateNumber>914403005571835420</CertificateNumber>
        <InvoiceSubBankName>测试20221014</InvoiceSubBankName>
        <CountryCode>CN</CountryCode>
        <SFASalesPath>全部,PRC Offline South,粤东,深圳市</SFASalesPath>
        <MarketingType>母婴-单店</MarketingType>
        <ChargePerson>Andy Liu(刘平)</ChargePerson>
        <InvoiceTaxNumber>636772617</InvoiceTaxNumber>
        <CATEGORYCODE>00</CATEGORYCODE>
        <MULTICODE>
          <VALUELIST>
            <PaymentAccountCode>30000866</PaymentAccountCode>
            <SPECIALITYCODE>PAYMENTINFO</SPECIALITYCODE>
            <LISTCODE>5I6FT34K4U5F7P0E23NT768513Y1661U</LISTCODE>
          </VALUELIST>
          <VALUELIST>
            <BussinessUnit>BU_SFPRCSH</BussinessUnit>
            <SPECIALITYCODE>FINANCEINFO</SPECIALITYCODE>
            <LISTCODE>43359CW4X0NMA89R88H46W66888SB2B8</LISTCODE>
          </VALUELIST>
          <VALUELIST>
            <ContactMobile>17365452345</ContactMobile>
            <IsPrimaryContact>是</IsPrimaryContact>
            <SfaContactId>9173566440704169777</SfaContactId>
            <SPECIALITYCODE>CONTACTINFO</SPECIALITYCODE>
            <LISTCODE>VE244A9Y3FNUWN7K747KFU6H6L84RHI5</LISTCODE>
            <ContactEMail>22133133@qq.com</ContactEMail>
            <ContactName>李迪</ContactName>
          </VALUELIST>
          <VALUELIST>
            <ContactMobile>17365459878</ContactMobile>
            <IsPrimaryContact>否</IsPrimaryContact>
            <SfaContactId>6788874131617504418</SfaContactId>
            <SPECIALITYCODE>CONTACTINFO</SPECIALITYCODE>
            <LISTCODE>6WQB8MA09Z4776I430SJPC2TU6K93UU4</LISTCODE>
            <ContactEMail>2233133@qq.com</ContactEMail>
            <ContactName>吴繁荣</ContactName>
          </VALUELIST>
          <VALUELIST>
            <SiteContactName>刘三</SiteContactName>
            <SiteAddress>测试77777</SiteAddress>
            <SfaSiteId>8480089929551622116</SfaSiteId>
            <SPECIALITYCODE>SITEINFO</SPECIALITYCODE>
            <SiteCityCode>110000</SiteCityCode>
            <LISTCODE>WAHVUTQXQP7S0GQ36Z562W0S9OZ3QYEG</LISTCODE>
            <SiteContactMobile>21878128337</SiteContactMobile>
          </VALUELIST>
          <VALUELIST>
            <SiteContactName>刘飒</SiteContactName>
            <SiteAddress>测试00333003</SiteAddress>
            <SfaSiteId>7037924554341270073</SfaSiteId>
            <SPECIALITYCODE>SITEINFO</SPECIALITYCODE>
            <SiteCityCode>330300</SiteCityCode>
            <LISTCODE>8Y2PT3L762Y3IRBP9Y3FSHX90320997H</LISTCODE>
            <SiteContactMobile>15876542345</SiteContactMobile>
          </VALUELIST>
        </MULTICODE>
        <FILES>
          <STOREPATH>https://image.waiqin365.com/6055786081413437802/cm/202210/ea56e72f4c2249ad8dc732517b36a634.jpg</STOREPATH>
          <FILETYPE>image/jpg</FILETYPE>
          <FILENAME>营业执照.jpg</FILENAME>
        </FILES>
        <FILES>
          <STOREPATH>https://image.waiqin365.com/6055786081413437802/cm/202301/59d94e4583cf42d2aa321694c8f5f483.jpg</STOREPATH>
          <FILETYPE>image/jpg</FILETYPE>
          <FILENAME>开票资料(图片).jpg</FILENAME>
        </FILES>
        <CustomerType>真实客户</CustomerType>
        <InvoiceType>增值税专用发票</InvoiceType>
        <TpmChannelType>Offline-GT</TpmChannelType>
        <CODE>10001267</CODE>
        <InvoiceAddress>测试20221014</InvoiceAddress>
        <TaxClassificationCode>CN VAT OUT 13</TaxClassificationCode>
        <Country>中国</Country>
        <FromDate>2022-10-14</FromDate>
        <SourceSystem>SFA</SourceSystem>
        <InvoiceName>测试20221014</InvoiceName>
      </DATAINFO>
    </DATAINFOS>
  </DATA>
</ESB>


博客评论
还没有人评论,赶紧抢个沙发~
发表评论
说明:请文明发言,共建和谐网络,您的个人信息不会被公开显示。