简体   繁体   中英

Enrich mediator add payload wso2

I used the enrich mediator to add a payload containing the name and totalnote of students my problem that i want to replace the values with the property

here is my code

  <property expression="get-property('uri.var.nom')" name="uri.var.nom" scope="default" type="STRING"/>
  <property expression="get-property('totalnote')" name="totalnote" scope="default" type="STRING"/>
        <enrich>
            <source clone="true" type="inline">
                {"nom":"" ,
                "note":""}
            </source>
            <target action="child" xpath="json-eval($)"/>
        </enrich>
        <enrich>
            <source clone="true" property="uri.var.nom" type="property"/>
            <target action="replace" xpath="json-eval($.etudiants.nom)"/>
        </enrich>
        <enrich>
            <source clone="true" property="totalnote" type="property"/>
            <target action="replace" xpath="json-eval($.etudiants.note)"/>
        </enrich>
        <respond/>

it doesn't work I always receive empty

{ "etudiants": { "nom": "", "note": "" }

You are placing the JSON structure at the root. As a child of $. But your structure does not contain etudiants, therefore the json-eval of $.etudiants.nom won't work.

The enrich itself works, as shown by @ycr but the message structure you assume to have is incorrect. Try logging the body after the first enrich to see what your payload looks like at that point.

Depending on your payload before the enrich try something like:

<enrich>
            <source clone="true" type="inline">
                {"etudiants": {
                "nom":"" ,
                "note":""
                }
            </source>
            <target action="replace" type="body"/>
        </enrich>

Or if you already have the 'etudiants' object maybe try adjusting the json-eval:

<enrich>
            <source clone="true" type="inline">
                {"nom":"" ,
                "note":""}
            </source>
            <target action="child" xpath="json-eval($.etudiants)"/>
        </enrich>

Hope this helps to clarify the situation.

The following seems to work for me. I have hardcoded the property values to test this. Also assuming the Payload before the Enrich is set as { "etudiants": { "nom": "", "note": "" }} . In the following example, I'm sending it in the request.

<?xml version="1.0" encoding="UTF-8"?>
<api context="/HelloWorld" name="HelloWorld" xmlns="http://ws.apache.org/ns/synapse">
    <resource methods="POST">
        <inSequence>
            <property name="uri.var.nom" scope="default" type="STRING" value="nomVal"/>
            <property name="totalnote" scope="default" type="STRING" value="20"/>
            <enrich>
                <source clone="true" property="uri.var.nom" type="property"/>
                <target xpath="json-eval($.etudiants.nom)"/>
            </enrich>
            <enrich>
                <source clone="true" property="totalnote" type="property"/>
                <target xpath="json-eval($.etudiants.note)"/>
            </enrich>
            <respond/>
        </inSequence>
        <outSequence/>
        <faultSequence/>
    </resource>
</api>

Request

curl --location --request POST 'http://localhost:8290/HelloWorld' \
--header 'Content-Type: application/json' \
--data-raw '{
    "etudiants": {
        "nom": "",
        "note": ""
    }
}'

Output

{
    "etudiants": {
        "nom": "nomVal",
        "note": 20
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM