WSO2 WebSocket无法解析JSON字符串

时间:2016-08-30 17:24:03

标签: json websocket wso2 wso2esb

由于WSO2 5.0支持WebSockets,我编写了一个简单的应用程序,后面是教程:

这是WSO2的源视图输出:

<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://ws.apache.org/ns/synapse">
    <registry provider="org.wso2.carbon.mediation.registry.WSO2Registry">
        <parameter name="cachableDuration">15000</parameter>
    </registry>
    <taskManager provider="org.wso2.carbon.mediation.ntask.NTaskTaskManager"/>
    <sequence name="fault">
        <!-- Log the message at the full log level with the ERROR_MESSAGE and the ERROR_CODE-->
        <log level="full">
            <property name="MESSAGE" value="Executing default 'fault' sequence"/>
            <property expression="get-property('ERROR_CODE')" name="ERROR_CODE"/>
            <property expression="get-property('ERROR_MESSAGE')" name="ERROR_MESSAGE"/>
        </log>
        <!-- Drops the messages by default if there is a fault -->
        <drop/>
    </sequence>
    <sequence name="main">
        <in>
            <!-- Log all messages passing through -->
            <log level="full"/>
            <!-- ensure that the default configuration only sends if it is one of samples -->
            <!-- Otherwise Synapse would be an open proxy by default (BAD!)               -->
            <filter regex="http://localhost:9000.*" source="get-property('To')">
                <!-- Send the messages where they have been sent (i.e. implicit "To" EPR) -->
                <send/>
            </filter>
        </in>
        <out>
            <send/>
        </out>
        <description>The main sequence for the message mediation</description>
    </sequence>
    <sequence name="outDispatchSeq">
        <log level="full">
            <property name="MESSAGE" value="Executing outDispatchSeq -------------- "/>
        </log>
        <respond/>
    </sequence>
    <sequence name="dispatchSeq">
        <payloadFactory media-type="json">
            <format>$1</format>
            <args>
                <arg evaluator="xml" expression="get-property('body')"/>
            </args>
        </payloadFactory>
        <property expression="json-eval($.testPropertyName)"
            name="testPropertyName" scope="default" type="STRING"/>
        <log level="full">
            <property name="MESSAGE" value="Executing dispatchSeq -------------- "/>
        </log>
    </sequence>
    <!-- You can add any flat sequences, endpoints, etc.. to this synapse.xml file if you do
    *not* want to keep the artifacts in several files -->
    <inboundEndpoint name="test" onError="fault" protocol="ws"
        sequence="dispatchSeq" suspend="false">
        <parameters>
            <parameter name="inbound.ws.port">9091</parameter>
            <parameter name="ws.outflow.dispatch.sequence">outDispatchSeq</parameter>
            <parameter name="ws.client.side.broadcast.level">0</parameter>
            <parameter name="ws.outflow.dispatch.fault.sequence">fault</parameter>
            <parameter name="content.type">application/json</parameter>
        </parameters>
    </inboundEndpoint>
</definitions>

然后我创建简单的JavaScript代码来连接WebSocket并发送简单的JSON数据:

function connect() {
    webSocket = new WebSocket('ws://localhost:9091/');


    webSocket.onopen = function(e) {
        console.log('connected: ' + webSocket);
    };

    // some logic here...
    webSocket.send("{ \"testPropertyName\": \"testValue\"}");
}

但是一直以来,WSO2都无法解析我的JSON对象。我收到的错误如下:

DEBUG - PayloadFactoryMediator #mediate. Transformed payload format>>>
ERROR - SynapseJsonPath #stringValueOf. Error evaluating JSON Path <$.testPropertyName>. Returning empty result. Error>>> invalid path
 INFO - LogMediator To: , MessageID: urn:uuid:93c8c0bc-2a77-4895-a852-1267e21f2044, Direction: request, MESSAGE = Executing dispatchSeq -------------- , Payload: {}
DEBUG - PayloadFactoryMediator #mediate. Transformed payload format>>>
ERROR - SynapseJsonPath #stringValueOf. Error evaluating JSON Path <$.testPropertyName>. Returning empty result. Error>>> invalid path
 INFO - LogMediator To: , MessageID: urn:uuid:828216c3-a3ba-476a-886d-302a62e117be, Direction: request, MESSAGE = Executing dispatchSeq -------------- , Payload: {}

更新

将loggin更改为:

log4j.logger.org.apache.synapse.transport.http.wire=DEBUG

并在payloadFactory之后添加loggig:

<sequence name="dispatchSeq">
    <payloadFactory media-type="json">
        <format>$1</format>
        <args>
            <arg evaluator="xml" expression="get-property('body')" />
        </args>
    </payloadFactory>
    <log level="full" />
    <property expression="json-eval($.testPropertyName)" name="testPropertyName" scope="default" type="STRING" />
    <log level="full">
        <property name="MESSAGE" value="Executing dispatchSeq -------------- " />
    </log>
</sequence>

WSO2正在记录以下输出:

DEBUG - PayloadFactoryMediator #mediate. Transformed payload format>>>
 INFO - LogMediator To: , MessageID: urn:uuid:7a04df37-b3c0-47a0-9ea1-78df460aec0a, Direction: request, Payload: {}
ERROR - SynapseJsonPath #stringValueOf. Error evaluating JSON Path <$.testPropertyName>. Returning empty result. Error>>> invalid path
 INFO - LogMediator To: , MessageID: urn:uuid:7a04df37-b3c0-47a0-9ea1-78df460aec0a, Direction: request, MESSAGE = Executing dispatchSeq -------------- , Payload: {}
DEBUG - PayloadFactoryMediator #mediate. Transformed payload format>>>
 INFO - LogMediator To: , MessageID: urn:uuid:9ef7732c-b196-435e-a800-5d98d670b11a, Direction: request, Payload: {}
ERROR - SynapseJsonPath #stringValueOf. Error evaluating JSON Path <$.testPropertyName>. Returning empty result. Error>>> invalid path
 INFO - LogMediator To: , MessageID: urn:uuid:9ef7732c-b196-435e-a800-5d98d670b11a, Direction: request, MESSAGE = Executing dispatchSeq -------------- , Payload: {}

更新

我删除了payloadFactory,如:

<sequence name="dispatchSeq">
    <log level="full" />
    <property expression="json-eval($.testPropertyName)" name="testPropertyName" scope="default" type="STRING" />
    <log level="full">
        <property name="MESSAGE" value="Executing dispatchSeq -------------- " />
    </log>
</sequence>

WSO2日志告诉我:

INFO - LogMediator To: , MessageID: urn:uuid:db00d7aa-69c8-4de6-8140-91169183c3d6, Direction: request, Envelope: <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body/></soapenv:Envelope>
WARN - SynapseJsonPath Json Payload is empty.
INFO - LogMediator To: , MessageID: urn:uuid:db00d7aa-69c8-4de6-8140-91169183c3d6, Direction: request, MESSAGE = Executing dispatchSeq -------------- , Envelope: <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body/></soapenv:Envelope>
INFO - LogMediator To: , MessageID: urn:uuid:19816ec6-c255-4edf-adae-cca83dbd2008, Direction: request, Envelope: <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body/></soapenv:Envelope>
WARN - SynapseJsonPath Json Payload is empty.
INFO - LogMediator To: , MessageID: urn:uuid:19816ec6-c255-4edf-adae-cca83dbd2008, Direction: request, MESSAGE = Executing dispatchSeq -------------- , Envelope: <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body/></soapenv:Envelope>

更新

修改后的dispatchSeq

<sequence name="dispatchSeq">
    <log level="full" />
    <property expression="json-eval($.testPropertyName)" name="testPropertyName" scope="default" type="STRING" />
    <log level="full">
        <property name="MESSAGE" value="Executing dispatchSeq -------------- " />
        <property expression="get-property('websocket.source.handshake.present')" name="websocket.source.handshake.present.value" scope="default" type="STRING"/>
    </log>
</sequence>

WSO2输出为:

INFO - LogMediator To: , MessageID: urn:uuid:5cc82e8d-6638-4cca-97f7-ac4f96877f85, Direction: request, Envelope: <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body/></soapenv:Envelope>
WARN - SynapseJsonPath Json Payload is empty.
INFO - LogMediator To: , MessageID: urn:uuid:5cc82e8d-6638-4cca-97f7-ac4f96877f85, Direction: request, MESSAGE = Executing dispatchSeq -------------- , websocket.source.handshake.present.value = true, Envelope: <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body/></soapenv:Envelope>
INFO - LogMediator To: , MessageID: urn:uuid:81b0e2bd-4e79-48bd-b177-f49fd5664c0a, Direction: request, Envelope: <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body/></soapenv:Envelope>
WARN - SynapseJsonPath Json Payload is empty.
INFO - LogMediator To: , MessageID: urn:uuid:81b0e2bd-4e79-48bd-b177-f49fd5664c0a, Direction: request, MESSAGE = Executing dispatchSeq -------------- , websocket.source.handshake.present.value = null, Envelope: <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body/></soapenv:Envelope>

你知道我还缺少什么吗? 我会很感激任何代码示例。

非常感谢!

0 个答案:

没有答案