给Json充实了不适合我的新元素。请指导

时间:2017-07-18 07:21:54

标签: wso2 wso2esb

给Json充实了不适合我的新元素。请指导。

输入请求:

 {
        "id" : "1",     
        "make" : "NAHB"
    }

我创建了名为eg1.xml的wso2流程。我试图用新元素“name”

来丰富传入的json请求

处理xml :( eg1.xml)

 <?xml version="1.0" encoding="UTF-8"?>
    <api context="/eg1" name="eg1" xmlns="http://ws.apache.org/ns/synapse">
        <resource methods="POST GET" uri-template="/hi">
            <inSequence>
                <log level="full"/>
                <log level="custom">
                    <property expression="//jsonObject" name="msg1"/>
                </log>
                <enrich description="">
                    <source clone="true" type="inline">
                        <name xmlns="">Home</name>
                    </source>
                    <target action="child" xpath="//jsonObject"/>
                </enrich>
                <log level="custom">
                    <property expression="//jsonObject" name="msg2"/>
                </log>
                <log level="full"/>
                <respond/>
            </inSequence>
            <outSequence/>
            <faultSequence/>
        </resource>
    </api>

当前输出:

 {
        "id" : "1",     
        "make" : "NAHB"
    }

期望的输出:

 {
        "id" : "1",     
        "make" : "NAHB"
        "name" : "Home"
    }

记录器输出:

Logger1输出(日志级别已满): LogMediator To:/ eg1 / hi,MessageID:urn:uuid:2f627daf-ac9c-4492-84ea-2736670724e8,Direction:request,Payload:

   {
        "id": "1",
        "make": "NAHB"
    }

Logger2输出: LogMediator msg1 =

 <jsonObject>
        <id>1</id>
        <make>NAHB</make>
    </jsonObject>

Logger3输出: LogMediator msg2 =

 <jsonObject>
        <id>1</id>
        <make>NAHB</make>
        <name>Home</name>
    </jsonObject>

Logger4输出(日志级别已满): LogMediator To:/ eg1 / hi,MessageID:urn:uuid:2f627daf-ac9c-4492-84ea-2736670724e8,Direction:request,Payload:

{     “id”:“1”,     “制造”:“NAHB” }

邮差输出:

 {
        "id": "1",
        "make": "NAHB"
    }

此致 阿迪亚

2 个答案:

答案 0 :(得分:1)

您必须将回复添加回正文

<api xmlns="http://ws.apache.org/ns/synapse" name="eg1" context="/eg1">
<resource methods="POST GET" uri-template="/hi">
  <inSequence>
     <log level="full"/>
     <log level="custom">
        <property name="msg1" expression="//jsonObject"/>
     </log>
     <enrich description="">
        <source type="inline" clone="true">
           <name xmlns="">Home</name>
        </source>
        <target action="child" xpath="//jsonObject"/>
     </enrich>
     <log level="full">
        <property name="msg2" expression="//jsonObject"/>
     </log>
     <enrich>
        <source clone="true" xpath="//jsonObject"/>
        <target type="body"/>
     </enrich>
     <log level="full"/>
     <respond/>
  </inSequence>
  <outSequence/>
  <faultSequence/>

我试过这个API

请求

 {
    "id" : "1",     
    "make" : "NAHB"
}

回复

{
   "id": 1,
   "make": "NAHB",
   "name": "Home"
}

答案 1 :(得分:0)

您有 3 种方法可以做到:

  1. 丰富介体:如其他答案中所述

  2. Payload Factory 中介:将需要在参数中添加所有参数,除非新元素将位于单独的子元素中

  3. 脚本中介:

                 <script language="js">
                 <![CDATA[
                     var payload = mc.getProperty('payloadProperty'); // if original json is in property. If received as payload use: mc.getPayloadJSON();
                    var json =  JSON.parse(payload);
                     var newElement = mc.getProperty('newElement'); // get the value of the new element
                     json.newElementName = newElement; // define it (newElementName not present in the json in the original payload
                     mc.setPayloadJSON(json); // put the result back as payload. Optional: read it in a property as done below if needed, otherwise just call the next service
                ]]>
            </script>
            <property expression="json-eval($)" name="json" scope="default" type="STRING"/>