Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用WSO2类中介转换JSON主体_Java_Json_Wso2_Wso2esb - Fatal编程技术网

Java 使用WSO2类中介转换JSON主体

Java 使用WSO2类中介转换JSON主体,java,json,wso2,wso2esb,Java,Json,Wso2,Wso2esb,下面是我当前json正文的日志。我想给这个身体添加新的属性“NewPropertyName”:“值”。由于该值位于数据库中,因此我使用类中介添加该属性 [2015-05-18 05:47:08,730] INFO - LogMediator To: /a/create-project, MessageID: urn:uuid:b7b6efa6-5fff-49be-a94a-320cee1d4406, Direction: request, _______BODY_______ = { "

下面是我当前json正文的日志。我想给这个身体添加新的属性<代码>“NewPropertyName”:“值”。由于该值位于数据库中,因此我使用类中介添加该属性

[2015-05-18 05:47:08,730]  INFO - LogMediator To: /a/create-project, MessageID: urn:uuid:b7b6efa6-5fff-49be-a94a-320cee1d4406, Direction: request, _______BODY_______ = 
{
  "token": "abc123",
  "usertype":"ext",
  "request": "create"
}
类中介的中介方法

public boolean mediate(MessageContext mc) {
        mc.setProperty("key", "vale retrived from db");
        return true;
}

但这并不像我预期的那样有效。我找不到任何使用类中介向json正文添加属性的指南,请提供帮助。

mc.setProperty用于创建新属性,就像您使用属性中介一样

如果您想在消息中添加新元素,在java中,您可以像处理XML消息一样处理它(例如,获取第一个元素:
OmeElement元素=(OmeElement)context.getEnvelope().getBody().getFirstOMChild();

使用javascript添加新元素的示例:

<script language="js"><![CDATA[
    var payloadXML = mc.getPayloadXML();
    payloadXML.appendChild(new XML(<NewPropertyName>value</NewPropertyName>));
    mc.setPayloadXML(payloadXML);
]]></script>
value];
mc.setPayloadXML(payloadXML);
]]>
使用
将消息记录在XML中,您会得到:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
  <soapenv:Body>
    <jsonObject>
      <token>abc123</token>
      <usertype>ext</usertype>
      <request>create</request>
      <NewPropertyName>value</NewPropertyName>
    </jsonObject>
  </soapenv:Body>
</soapenv:Envelope>

abc123
提取
创造
价值
使用JSON记录消息

<log>
  <property name="JSON-Payload" expression="json-eval($.)"/>
</log> 

你会得到:
JSON有效负载=
{“token”:“abc123”,“usertype”:“ext”,“request”:“create”,“NewPropertyName”:“value”}

要将属性注入主体,必须使用以下代码段

JsonUtil.newJsonPayload(
            ((Axis2MessageContext) context).getAxis2MessageContext(),
            transformedJson, true, true);
在类中介中。下面是中介方法的一个示例

/**
 * Mediate overridden method to set the token property.
 */@Override
public boolean mediate(MessageContext context) {
try {

    // Getting the json payload to string
    String jsonPayloadToString = JsonUtil.jsonPayloadToString(((Axis2MessageContext) context)
        .getAxis2MessageContext());
    // Make a json object
    JSONObject jsonBody = new JSONObject(jsonPayloadToString);

    // Adding the name:nameParam.
    jsonBody.put("name", getNameParam());

    String transformedJson = jsonBody.toString();

    // Setting the new json payload.
    JsonUtil.newJsonPayload(
    ((Axis2MessageContext) context).getAxis2MessageContext(),
    transformedJson, true, true);

    System.out.println("Transformed JSON body:\n" + transformedJson);

} catch (Exception e) {
    System.err.println("Error occurred: " + e);
    return false;
}

return true;
}
为此,您需要json和其他库。这在下面的博客文章中得到了充分的解释


我在类中介器中添加了元素(通过打印xml确认),但当我再次使用日志中介器打印它时,它没有显示在json正文中,json主体没有改变。我已经用基于JavaScriptor的示例更新了我的答案。感谢您的帮助,但请注意,我正在使用类中介,我想在类中介中转换它,因为我需要一些复杂的任务。有更新吗?我们也有同样的问题。@MiladKianmehr Yep,我已经解决了这个问题,并为此线程添加了一个新的答案。请查看新的答案。我不能使用脚本中介,因为我们有很大的负载和代码长度错误。@MiladKianmehr新的答案添加了解释如何使用类中介进行此操作。我想那会对你有帮助的。谢谢你,伊苏鲁。此url现在不可用。你能给出我需要添加的依赖项吗task@Dev4World尝试此处:此url无效。你查过了吗?上面说isurugunawardana.com最近过期了!哦,不@Dev4World它现在应该可以工作了。如果没有,几个小时后就会有。好的,我查一下。谢谢你,伊苏鲁