[英]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.