WSO2自定义处理程序使用JSON数据

时间:2018-05-23 11:46:59

标签: wso2 wso2-am

在自定义WSO2处理程序上编写我的服务使用JSON。试图让处理程序读取JSON数据

Howto extract data from the JSON body of REST request inside a WSO2 ESB Synapse handler中的求解 没用?

enter image description here

处理程序代码

@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"/-->

任何帮助都会有很大的帮助

由于

2 个答案:

答案 0 :(得分:0)

在打印正文之前尝试RelayUtils.buildMessage(messageContext);

答案 1 :(得分:0)

为您的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);