简体   繁体   English

WSO2自定义处理程序可处理JSON数据

[英]WSO2 custom handler work with JSON data

Writing on a custom WSO2 handler my services work using JSON. 在自定义WSO2处理程序上编写代码后,我的服务将使用JSON进行工作。 Trying to get the handler read JSON data 试图让处理程序读取JSON数据

The solve in Howto extract data from the JSON body of REST request inside a WSO2 ESB Synapse handler did not work 如何从WSO2 ESB Synapse处理程序内的REST请求的JSON正文提取数据的解决方案不起作用

在此处输入图片说明

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 尝试了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"/-->

Any help will be of great help 任何帮助都会有很大帮助

Thanks 谢谢

Try RelayUtils.buildMessage(messageContext); 尝试RelayUtils.buildMessage(messageContext); before printing the body. 在打印主体之前。

Try this code for your json: 为您的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.

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