我正在尝试使用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>
到目前为止,我已经尝试过但没有用的东西:
关于如何使其正常工作的任何想法?
您可以参考以下更改有效负载的逻辑
@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;
}
如果这没有帮助,请分享您的代码以获取想法。
我已经尝试过该教程@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。