Howto extract data from the JSON body of REST request inside a WSO2 ESB Synapse handler

谁都会走 提交于 2019-12-24 16:31:05

问题


I'm writing a custom Handler for WSO2 ESB to construct authentication credentials based on input request content. Right now what I have is something like this:

public boolean handleRequest(MessageContext context) {
    // TODO: extract relevant information (clientId) from JSON request body
    String clientId;

    Map<String, String> headers = (Map<String, String>) ((Axis2MessageContext) context).getAxis2MessageContext().getProperty(
            org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);

    setAuthorization(headers, clientId);

    return true;
}

I can't find documentation regarding howto access REST JSON request body inside the Synapse handler. Any ideas? Is possible to define a property before the handler runs and capture it with something like String clientId = (String)context.getProperty("clientId")?


回答1:


You can try following;

// Getting the json payload to string
String jsonPayloadToString = JsonUtil.jsonPayloadToString(((Axis2MessageContext) context).getAxis2MessageContext());
// Make a json object
JSONObject jsonBody = new JSONObject(jsonPayloadToString);


来源:https://stackoverflow.com/questions/35635750/howto-extract-data-from-the-json-body-of-rest-request-inside-a-wso2-esb-synapse

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!