【问题标题】:WSO2 ESB Message context cannot be changed through custom class mediatorWSO2 ESB 消息上下文无法通过自定义类调解器更改
【发布时间】:2020-02-12 15:13:33
【问题描述】:

我正在尝试使用 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


    【解决方案1】:

    你可以参考下面改变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;
    }
    

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

    【讨论】:

      【解决方案2】:

      我已经尝试过@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 应该绰绰有余。

      【讨论】:

      • 是否可以打印一些日志来验证度量值和类中介本身内部的有效负载,以确保一切正常。您可以在类中介器中打印 mc.getEnvelope().getBody() 以检查有效负载是否实际更改
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多