使用Property Mediator时如何获取json属性?

时间:2019-12-03 15:17:01

标签: wso2

我使用Property Mediator获取注册表资源,它返回了一个json字符串,但是如何在json字符串中获取该属性? 我的代码示例:

test-file.json like so
{
    "mappings": {
        "mapping": {
            "ep_1": "http://localhost:8280/services/ep_1",
            "ep_2": "http://localhost:8280/services/ep_2",
            "ep_3": "http://localhost:8280/services/ep_3"
        }
    }
}

我喜欢这样:


<property expression="get-property('registry','conf:customresource/test-file.json')" name="JsonContent" scope="default" type="STRING"/>
<property expression="????" name="endpointUrl" />

如何在'endpointUrl'中获得属性'ep_1'还是有其他方法来获得属性'ep_1'?

3 个答案:

答案 0 :(得分:2)

尝试以下操作。

expression="json-eval($ctx:JsonContent.mappings.mapping.ep_1)"

如果上述方法无效,请尝试此操作。

expression="$ctx:JsonContent//mappings/mapping/ep_1"

答案 1 :(得分:0)

您可以将json文件从注册表加载到有效负载,并在有效负载上进行json-eval。这是肮脏的解决方案,但是可以用;):

function addPopup(feature, layer) {
    var popupContent = feature.properties.name;
    layer.bindPopup(popupContent);

    layer.on('mouseover', function (e) {
        this.openPopup();
    });
    layer.on('mouseout', function (e) {
        this.closePopup();
    });
}

最诚挚的问候

答案 2 :(得分:0)

我已经解决了这个问题。您必须使用XML内容而不是JSON,然后将内容设置为类型为OM的Property Mediator,并且可以使用xpath表达式获取XML内容中所需的任何值。 代码示例

XML content:
<mappings>
    <mapping>
        <ep_1>http://localhost:8280/services/ep_1</ep_1>
        <ep_2>http://localhost:8280/services/ep_2</ep_2>
        <ep_3>http://localhost:8280/services/ep_3</ep_3>
    </mapping>
</mappings>
<property expression="get-property('registry','conf:customresource/test-file.xml')" name="XmlContent" scope="default" type="OM"/>
<property expression="$ctx:XmlContent/mapping/ep_1" name="endpointUrl" />

之后,该值将被设置到名为endpointUrl的属性中。 最后,请注意第二个Property mediator的表达式,如果您喜欢$ ctx:XmlContent / mappings / mapping / ep_1,则会获得黑色值。希望对他人有所帮助。