[英]WSO2 custom handler work with JSON data
在自定義WSO2處理程序上編寫代碼后,我的服務將使用JSON進行工作。 試圖讓處理程序讀取JSON數據
如何從WSO2 ESB Synapse處理程序內的REST請求的JSON正文提取數據的解決方案不起作用
處理程序代碼
@Override
public boolean handleRequest(MessageContext messageContext) {
System.out.println("getEnvelope - "+ messageContext.getEnvelope().getBody().toString());
org.apache.axis2.context.MessageContext mc = ((Axis2MessageContext) messageContext).getAxis2MessageContext();
JSONObject jsonBody = new JSONObject(JsonUtil.jsonPayloadToString(mc));
System.out.println("Payload in json -"+ jsonBody);
String jsonPayloadToString = JsonUtil.jsonPayloadToString(((Axis2MessageContext) messageContext).getAxis2MessageContext());
System.out.println("Payload in string -"+ jsonPayloadToString);
控制台輸出
getEnvelope - <soapenv:Body xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"/>
Payload in json -{}
Payload in string -{}
嘗試了axis2.xml中的所有3種三種組合
<messageBuilder contentType="application/json"
class="org.apache.axis2.json.JSONOMBuilder"/>
<!--messageBuilder contentType="application/json"
class="org.apache.synapse.commons.json.JsonStreamBuilder"/-->
<!--messageBuilder contentType="application/json"
class="org.apache.synapse.commons.json.JsonBuilder"/-->
-----------------
<messageFormatter contentType="application/json"
class="org.apache.axis2.json.JSONMessageFormatter"/>
<!--messageFormatter contentType="application/json"
class="org.apache.synapse.commons.json.JsonStreamFormatter"/-->
<!--messageFormatter contentType="application/json"
class="org.apache.synapse.commons.json.JsonFormatter"/-->
任何幫助都會有很大幫助
謝謝
嘗試RelayUtils.buildMessage(messageContext);
在打印主體之前。
為您的json嘗試以下代碼:
try {
RelayUtils.buildMessage(((Axis2MessageContext) messageContext).getAxis2MessageContext());
}
catch (XMLStreamException e) {
e.printStackTrace();
}
catch (IOException e1) {
e1.printStackTrace();
}
String body = JsonUtil.jsonPayloadToString(((Axis2MessageContext) messageContext).getAxis2MessageContext());
String httpMethod = (String) ((Axis2MessageContext) messageContext).getAxis2MessageContext().getProperty("HTTP_METHOD");
System.out.println("\n\nWSO2CustomHandler - handleRequest body!!" + body);
System.out.println("\n\nWSO2CustomHandler - handleRequest httpMethod!!" + httpMethod);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.