我正在尝试使用 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>
到目前为止我尝试过但不起作用的事情:
- 获取消息上下文,获取所需元素并设置文本
- 使用 OMFactory 创建 OMElement 并将该新元素放入消息上下文中
- 获取新的更改后的信封并将其设置为新的消息上下文信封
- 创建新的 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/