Я использовал посредник обогащения, чтобы добавить полезную нагрузку, содержащую имя и общее количество студентов. моя проблема в том, что я хочу заменить значения свойством
вот мой код
<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/>
это не работает, я всегда получаю пустой
{ "etudiants": { "nom": "", "note": "" }
Следующее, кажется, работает для меня. Я жестко запрограммировал значения свойств, чтобы проверить это. Также предполагается, что полезная нагрузка до обогащения установлена как { "etudiants": { "nom": "", "note": "" }}
. В следующем примере я отправляю его в запросе.
<?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>
Запрос
curl --location --request POST 'http://localhost:8290/HelloWorld' \
--header 'Content-Type: application/json' \
--data-raw '{
"etudiants": {
"nom": "",
"note": ""
}
}'
Вывод
{
"etudiants": {
"nom": "nomVal",
"note": 20
}
}
Вы помещаете структуру JSON в корень. В детстве $. Но ваша структура не содержит etudiants, поэтому json-eval $.etudiants.nom не сработает.
Само обогащение работает, как показано @ycr, но структура сообщения, которую вы предполагаете, неверна. Попробуйте зарегистрировать тело после первого обогащения, чтобы увидеть, как выглядит ваша полезная нагрузка в этот момент.
В зависимости от вашей полезной нагрузки перед обогащением попробуйте что-то вроде:
<enrich>
<source clone = "true" type = "inline">
{"etudiants": {
"nom":"" ,
"note":""
}
</source>
<target action = "replace" type = "body"/>
</enrich>
Или, если у вас уже есть объект «ученики», попробуйте настроить json-eval:
<enrich>
<source clone = "true" type = "inline">
{"nom":"" ,
"note":""}
</source>
<target action = "child" xpath = "json-eval($.etudiants)"/>
</enrich>
Надеюсь, это поможет прояснить ситуацию.