WSO2 ESB消息上下文无法通过自定义类介体进行更改

问题描述 投票:0回答:2

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

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有效负载

关于如何使其正常工作的任何想法?

java json wso2 wso2esb mediator
2个回答
0
投票

您可以参考以下更改有效负载的逻辑

@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;
}

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


0
投票

我已经尝试过该教程@Nirothipan,但是没有用。

我的代码:

@Override
public boolean mediate(MessageContext mc){

    String measure = mc.getEnvelope().getBody().getFirstElement().getFirstChildWithName(new QName("measure")).getText();
    mc.getEnvelope().getBody().getFirstElement().getFirstChildWithName(new QName("temp")).setText(measure);

    return true;
}

应该足以修改该元素值imo。

© www.soinside.com 2019 - 2024. All rights reserved.