在Dataweave中使用多个Json输入时出现意外的字符错误

时间:2017-01-15 23:18:09

标签: java json mule anypoint-studio dataweave

我有一个Json有效载荷和Json flowVars。我使用Products Json flowVar在Stores Json有效负载中填充Products数组,但是我看到以下错误:

INFO  2017-01-15 23:06:32,559 [[test].test-httpListenerConfig.worker.01] org.mule.api.processor.LoggerMessageProcessor: **** Stores JSON Payload: {"storeId":"1234"}
INFO  2017-01-15 23:06:32,574 [[test].test-httpListenerConfig.worker.01] org.mule.api.processor.LoggerMessageProcessor: *** Products JSON flowvar{"products": [{   "product": "phone",   "price": "499.99"  }]}
ERROR 2017-01-15 23:06:34,489 [[test].test-httpListenerConfig.worker.01] org.mule.exception.DefaultMessagingExceptionStrategy: 
********************************************************************************
Message               : Exception while executing: 
{stores={storeId=1234}, products=[]}
 ^
Unexpected character 's' at index 1 (line 1, position 2), expected '"'.
Payload               : {"storeId":"1234"}
Payload Type          : java.lang.String
Element               : /getFlow/processors/3/3 @ test:testcsv.xml:22 (Transform Message)
Element XML           : <dw:transform-message metadata:id="43b88f1f-032a-442f-94f7-09fbc853390b" doc:name="Transform Message">
                        <dw:input-payload mimeType="application/json"></dw:input-payload>
                        <dw:input-variable mimeType="application/json" variableName="varTest"></dw:input-variable>
                        <dw:set-payload>%dw 1.0%input payload application/json%output application/json---{products: flowVars.varTest.products map ((product , indexOfProduct) -> {product: product.product,price: product.price})}</dw:set-payload>
                        </dw:transform-message>
--------------------------------------------------------------------------------
Root Exception stack trace:
com.mulesoft.weave.mule.exception.WeaveExecutionException: Exception while executing: 
{stores={storeId=1234}, products=[]}
 ^
Unexpected character 's' at index 1 (line 1, position 2), expected '"'.
    at com.mulesoft.weave.mule.exception.WeaveExecutionException$.apply(WeaveExecutionException.scala:12)
    at com.mulesoft.weave.mule.WeaveMessageProcessor.execute(WeaveMessageProcessor.scala:121)
    at com.mulesoft.weave.mule.WeaveMessageProcessor.process(WeaveMessageProcessor.scala:67)

记录器将有效负载和flowVar显示为json格式,但错误看起来像是将其作为hashmap读取。不确定导致错误的原因是什么?

测试网址

http://localhost:8083/api/test

XML流程

    <?xml version="1.0" encoding="UTF-8"?>

    <mule xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw" xmlns:metadata="http://www.mulesoft.org/schema/mule/metadata" xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:mongo="http://www.mulesoft.org/schema/mule/mongo" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
        xmlns:spring="http://www.springframework.org/schema/beans" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
    http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
    http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd
    http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
    http://www.mulesoft.org/schema/mule/mongo http://www.mulesoft.org/schema/mule/mongo/current/mule-mongo.xsd
    http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd
    http://www.mulesoft.org/schema/mule/ee/dw http://www.mulesoft.org/schema/mule/ee/dw/current/dw.xsd">
        <flow name="getFlow">
            <http:listener config-ref="testdata-httpListenerConfig" path="/test" doc:name="HTTP"/>
        <set-variable variableName="varTest" value="{ &quot;products&quot;: [{      &quot;product&quot;: &quot;phone&quot;,         &quot;price&quot;: &quot;499.99&quot;   }]}" mimeType="application/json" doc:name="Variable"/>
        <set-payload value="[{  &quot;stores&quot;: {   &quot;storeId&quot;: &quot;1234&quot;  } }]" mimeType="application/json" doc:name="Set Payload"/>
        <json:json-to-object-transformer returnClass="java.lang.Object" doc:name="JSON to Object"/>
        <foreach doc:name="For Each" collection="#[payload]">
            <json:object-to-json-transformer mimeType="application/json" doc:name="Object to JSON"/>
            <logger message="#['**** Stores JSON Payload: ' + payload]" level="INFO" doc:name="Logger"/>
            <logger message="#['*** Products JSON flowvar' + flowVars.varTest]" level="INFO" doc:name="Logger"/>
            <dw:transform-message metadata:id="43b88f1f-032a-442f-94f7-09fbc853390b" doc:name="Transform Message">
                <dw:input-payload mimeType="application/json" doc:sample="C:\getStores.json"/>
                <dw:input-variable mimeType="application/json" variableName="varTest" doc:sample="sample_data\list_json_15.json"/>
                <dw:set-payload><![CDATA[%dw 1.0
%input payload application/json
%input in1 application/json
%output application/json
---
{
    stores: {
        storeId: payload.stores.storeId
    },
    products: flowVars.varTest.products map ((product , indexOfProduct) -> {
        product: product.product,
        price: product.price
    })
}]]></dw:set-payload>
            </dw:transform-message>                
            <logger message="#[payload]" level="INFO" doc:name="Logger"/>
            </foreach>
        </flow>
    </mule>

存储JSON有效负载:

{
    "stores": {
        "storeId": "1234"
    },
    "products": []
}

产品

{
    "products": [{
        "product": "phone",
        "price": "499.99"
    }]
}

预期结果

{
    "stores": {
        "storeId": "1234"
    },
    "products": [{
        "product": "phone",
        "price": "499.99"
    }]
}

由于

1 个答案:

答案 0 :(得分:0)

感谢您查看此内容。我找到了解决方案。

当我更新我的foreach集合以指定json:而不是有效负载<foreach collection="json:" doc:name="For Each">然后我发现它成功完成并且它可以遍历循环而无需将其转换为hashmap的Java列表。

我仍然有点困惑,为什么这会有效,因为即使我转换为Java来遍历列表,我也使用Object to JSON转换器将有效负载转换回JSON,但看起来它在这个实例中没有真正转换过flow仍将其视为Java。