1

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

enter image description here

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

Mahesh Siva S T
  • 175
  • 1
  • 2
  • 9
  • Try `RelayUtils.buildMessage(messageContext);` before printing the body. And use streambuilder/formatter in axis2.xml file. – Bee May 23 '18 at 12:19

2 Answers2

0

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

Bee
  • 12,251
  • 11
  • 46
  • 73
  • Thanks for the reply @Bee Still no luck `org.apache.axis2.context.MessageContext mc = ((Axis2MessageContext) messageContext).getAxis2MessageContext(); RelayUtils.buildMessage(mc); Sysout("getEnvelope - "+ mc.getEnvelope().getBody().toString()); JSONObject jsonBody = new JSONObject(JsonUtil.jsonPayloadToString(mc)); Sysout("Payload in json -"+ jsonBody); Sysout("Payload in xml -"+ mc); String jsonPayloadToString = JsonUtil.jsonPayloadToString(mc); Sysout("Payload in string -"+ jsonPayloadToString);` – Mahesh Siva S T May 24 '18 at 09:52
  • output `getEnvelope - 8599947105184598 Payload in json -{} Payload in xml -[MessageContext: logID=813d1f194b272676ca200341376a57e75024074be1035574] Payload in string -{}` config` ` kindly help – Mahesh Siva S T May 24 '18 at 09:53
0

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);
hello1693
  • 99
  • 3
  • 12