java - WSO2 ESB 消息上下文无法通过自定义类中介器更改

标签 java json wso2 mediator wso2-esb

我正在尝试使用 wso2 esb 中的自定义类中介器来更改消息。我想要实现的是在发送的消息中添加/设置元素的值。该消息使用 REST API 发送,并通过提到的类(发生转换的地方)。但是,当我在类(class)结束后对消息进行完整日志记录时,我发现该消息保留了与最初相同的值(基本上,该类仅在类中介器中时更改消息,因此当它从中介者,它返回到原来的输入形式)。

Input:
Body : <soapenv:Body ...><jsonObject><ts>2020-01-13</ts><temp></temp></jsonObject></soapenv:Body>

Desired output:
Body : <soapenv:Body ...><jsonObject><ts>2020-01-13</ts><temp>Hello</temp></jsonObject></soapenv:Body>

到目前为止我尝试过但不起作用的事情:

  1. 获取消息上下文,获取所需元素并设置文本
  2. 使用 OMFactory 创建 OMElement 并将该新元素放入消息上下文中
  3. 获取新的更改后的信封并将其设置为新的消息上下文信封
  4. 创建新的 json 负载

知道如何让它工作吗?

最佳答案

可以引用下面改变payload的逻辑

@Override

  public boolean mediate(MessageContext messageContext) {

try {

org.apache.axis2.context.MessageContext axis2MessageContext = ((Axis2MessageContext)messageContext).getAxis2MessageContext();

JSONObject jsonBody = new JSONObject();
JSONObject jsonError = new JSONObject();
jsonError.put("error","Authoraization Missing");
jsonError.put("detail","Authoraization Credentials invalid");
jsonError.put("title","Authoraization Error");

jsonBody.put("status", "403");
jsonBody.put("errorMessage", jsonError);

String transformedJson = jsonBody.toString();

JsonUtil.newJsonPayload(axis2MessageContext,transformedJson,  true, true);
 // change the response type to XML
 axis2MessageContext.setProperty("messageType", "application/xml");
 axis2MessageContext.setProperty("ContentType", "application/xml");

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

如果这没有帮助,请分享您的代码以获取想法。

关于java - WSO2 ESB 消息上下文无法通过自定义类中介器更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60191454/

相关文章:

java套接字编程

java - 如何在多条线交叉的矩形内找到区域?

javascript - 如何在ajax中执行if else将数据传递给json

api - WSO2 API 管理器 : Configuration of the access-files consumed services

security - 安全领域的定义

java - Google App Engine Java 从备用电子邮件地址发送邮件

java - .java 报告生成、方法调用者和被调用者

javascript - 如何在 three.js 中正确加载 json 模型?

java - 从 Java 中的嵌套空对象创建示例 JSON

java - 无法接合壁垒模块。 WSO2 IS 客户端示例