简体   繁体   中英

WSO2 custom handler work with JSON data

Writing on a custom WSO2 handler my services work using JSON. Trying to get the handler read JSON data

The solve in Howto extract data from the JSON body of REST request inside a WSO2 ESB Synapse handler did not work

在此处输入图片说明

Handler code

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

console Output

getEnvelope - <soapenv:Body xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"/>                                                                                                                      
Payload in json -{}                                                                                                                                                                                          
Payload in string -{}   

Tried the all 3 three combinations in axis2.xml

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

Any help will be of great help

Thanks

Try RelayUtils.buildMessage(messageContext); before printing the body.

Try this code for your 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM