使用WSO2类中介转换JSON主体

Isu*_*ana 5 java json wso2 wso2esb

以下是我当前json体的日志.我想为这个机构增加新的属性."NewPropertyName": "value".由于值在数据库中,因此我使用类中介来添加此属性.

[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"
}
Run Code Online (Sandbox Code Playgroud)

班级调解员的中介方法,

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

但这并不像我预期的那样有效.我找不到任何使用类介体向json body添加属性的指南,请帮忙.

Isu*_*ana 9

要向正文注入属性,您必须使用以下代码段,

JsonUtil.newJsonPayload(
            ((Axis2MessageContext) context).getAxis2MessageContext(),
            transformedJson, true, true);
Run Code Online (Sandbox Code Playgroud)

在班级调解员里面.以下是中介方法的示例.

/**
 * 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;
}
Run Code Online (Sandbox Code Playgroud)

你需要json和其他库.以下博客文章对此进行了详细说明.

JSON支持换WSO2 ESB的类,调解员