简体   繁体   English

使用WSO2类中介转换JSON主体

[英]Transforming JSON body using WSO2 class mediator

Following is the log of my current json body. 以下是我当前json体的日志。 And I want to add new property to this body. 我想为这个机构增加新的属性。 "NewPropertyName": "value" . "NewPropertyName": "value" Since the value is in a database I am using a class mediator to add this property. 由于值在数据库中,因此我使用类中介来添加此属性。

[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"
}

Class mediator's mediate method, 班级调解员的中介方法,

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

but this doesn't work as I expected. 但这并不像我预期的那样有效。 I couldn't find any guide to add property to json body using class mediator, please help. 我找不到任何使用类介体向json body添加属性的指南,请帮忙。

To inject a property to the body you have to use following code snippet, 要向正文注入属性,您必须使用以下代码段,

JsonUtil.newJsonPayload(
            ((Axis2MessageContext) context).getAxis2MessageContext(),
            transformedJson, true, true);

inside a class mediator. 在班级调解员里面。 Following is an example of mediate method. 以下是中介方法的示例。

/**
 * 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;
}

You will need json and other libraries for this. 你需要json和其他库。 This is fully explained in following blog post. 以下博客文章对此进行了详细说明。

json-support-for-wso2-esb-class-mediator JSON支持换WSO2 ESB的类,调解员

mc.setProperty is used to create a new property as if you were using property mediator. mc.setProperty用于创建新属性,就像使用属性介体一样。

If you want to add a new element inside your message, in java, you can handle it as if it were a XML message (for exemple, to get the first element : 如果你想在你的消息中添加一个新元素,在java中,你可以像处理XML消息一样处理它(例如,获取第一个元素:
OMElement element = (OMElement) context.getEnvelope().getBody().getFirstOMChild(); )

Sample to add a new element with a javascript : 使用javascript添加新元素的示例:

<script language="js"><![CDATA[
    var payloadXML = mc.getPayloadXML();
    payloadXML.appendChild(new XML(<NewPropertyName>value</NewPropertyName>));
    mc.setPayloadXML(payloadXML);
]]></script>

Log the message in XML with <log level="full"> and you get : 使用<log level="full">以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>

Log the message in JSON with 使用JSON记录消息

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

and you get : JSON-Payload = {"token":"abc123","usertype":"ext","request":"create","NewPropertyName":"value"} 你得到:JSON-Payload = {"token":"abc123","usertype":"ext","request":"create","NewPropertyName":"value"}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ESB WSO2 Java - 我想使用 Class Mediator 修改 json 文件中的值 - ESB WSO2 Java - I want to modify a value from a json file with Class Mediator 使用类调解器在WSO2 ESB中创建自定义代理时发生错误 - Error occurred when create custom proxy in WSO2 ESB using class mediator 我需要从wso2类调解器调用chat sdk - I need to call chat sdk from wso2 class mediator 如何将 boolean 属性传递给 WSO2 EI 中的 class 中介 - How to pass boolean property to class mediator in WSO2 EI 在 WSO2 APIM 3.2.0 中修改/删除 wso2 类中介或 xml 序列中的请求有效负载内容 - Modify/Remove request payload content in wso2 class mediator or xml sequence in WSO2 APIM 3.2.0 使用 CAR 将自定义中介部署到 WSO2 ESB - Deploy a custom mediator to WSO2 ESB using a CAR WSO2 ESB 错误部署自定义中介 - WSO2 ESB error on deploying custom mediator WSO2 class 调解员:Amazon aws import for sqs queue not being recognized - WSO2 class mediator: Amazon aws import for sqs queue not being recognized 无法在 class mediator 和 xml wso2 apim 3.2.0 中设置自定义响应码 - Unable to set the custom response code in class mediator and xml wso2 apim 3.2.0 WSO2 ESB 消息上下文无法通过自定义类调解器更改 - WSO2 ESB Message context cannot be changed through custom class mediator
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM