骡JSON到对象的变压器和变压器Dataweaver异常(Mule json-to-object-t

2019-09-29 08:30发布

我有一个问题用骡子Dataweave变压器获得

INFO  2017-08-10 15:05:52,787 [amqpReceiver.02] org.mule.api.processor.LoggerMessageProcessor: Authorisation Changed Event received. authorisation id: 1
INFO  2017-08-10 15:05:57,844 [amqpReceiver.02] org.mule.api.processor.LoggerMessageProcessor: type before class com.mulesoft.weave.reader.ByteArraySeekableStream
INFO  2017-08-10 15:06:02,058 [amqpReceiver.02] org.mule.api.processor.LoggerMessageProcessor: type after: class com.mulesoft.weave.reader.ByteArraySeekableStream
INFO  2017-08-10 15:06:13,309 [amqpReceiver.02] org.mule.api.processor.LoggerMessageProcessor: Org Name :- Id:- Org Name 3
ERROR 2017-08-10 15:06:15,520 [amqpReceiver.02] org.mule.exception.RollbackMessagingExceptionStrategy: 
********************************************************************************
Message               : Exception while executing: 
{
^
Unexpected end-of-input at index 0 (line 1, position 1), expected JSON Value
Payload               : com.mulesoft.weave.reader.ByteArraySeekableStream@a622be8
Element               : /subscribe-to-changeFlow/processors/8 @ voa-009-mule-authorisation-search-api:test.xml:60 (Final Transform Message)
Element XML           : <dw:transform-message doc:name="Final Transform Message" metadata:id="fa7e7663-50bb-4675-a8c8-e8cb311946a9">
                        <dw:input-payload mimeType="application/json"></dw:input-payload>
                        <dw:set-payload resource="classpath:dwl/owner-authorisation-enrichment.dwl"></dw:set-payload>
                        </dw:transform-message>
--------------------------------------------------------------------------------
Root Exception stack trace:
com.mulesoft.weave.reader.json.JsonReaderException: Unexpected end-of-input at index 0 (line 1, position 1), expected JSON Value
    at com.mulesoft.weave.reader.json.JsonTokenizer.fail(JsonTokenizer.scala:193)
    at com.mulesoft.weave.reader.json.JsonTokenizer.readValue(JsonTokenizer.scala:49)
    at com.mulesoft.weave.reader.json.JsonTokenizer.tokens(JsonTokenizer.scala:16)
    at com.mulesoft.weave.reader.json.JsonReader.root(JsonReader.scala:17)
    at com.mulesoft.weave.mule.reader.ReusableReader.root(ReusableReader.scala:12)
    at com.mulesoft.weave.engine.EvaluationContext$InternalExecutionContext$$anonfun$3.apply(EvaluationContext.scala:106)
  • 流接收ID作为消息有效载荷
  • 使HTTP调用获得一些JSON的ID
  • 并初步Dataweave转换成JSON转换到一个新的JSON模式
  • 为了提取从JSON一些值我使用JSON:JSON到对象的变压器,但我已经把这个在富集靶向flowVar允许转换为地图,并保持原有的有效载荷不变

      <enricher target="#[flowVars.ownerAuthorisationJsonMap]" doc:name="Copy to JSON map"> <json:json-to-object-transformer returnClass="java.util.HashMap" doc:name="JSON to Object"/> </enricher> 
  • 然后我叫子流,以获得一些进一步的日期晚充实。 该子流只存储在另一个flowVar的富集的结果

  • 第二Dataweave变压器(获得除外)需要从第1 Dataweave变压器JSON模式输出,丰富了使用数据的一些缺失值抬头通过子流(例子中之一,但会有很多的地图)。 这得到了异常。

但是,如果我注释掉嵌套JSON到对象的变压器 浓缩塔

         <enricher target="#[flowVars.ownerAuthorisationJsonMap]"  doc:name="Copy to JSON map"> 
             <json:json-to-object-transformer returnClass="java.util.HashMap" doc:name="JSON to Object"/>
        </enricher> 

第二dataweave工作正常 (但我不能在JSON访问数据)。 看来JSON到对象的变压器冷门的东西,但如果我调试的负载类型和Java对象ID贯穿保持不变。

我只使用JSON到对象的变压器,以MEL访问JSON作为似乎JSON路径已被弃用被告知。

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

<amqp:endpoint name="authorisationChangeQueueDef"
    queueName="authorisation-change-queue" queueDurable="true"
    exchangeName="authorisation-exchange" exchangeType="direct" exchangeDurable="true"
    connector-ref="AMQP_Connector"
    routingKey="authorisation-change-queue-routing-key" doc:name="authorisationChangeQueueDef">
       <properties>
            <spring:entry key="amqp-queue.x-dead-letter-exchange"    value="authorisation-exchange" />
            <spring:entry key="amqp-queue.x-dead-letter-routing-key" value="authorisation-change-queue-dead-routing-key" />
        </properties>
</amqp:endpoint>


<flow name="subscribe-to-changeFlow" processingStrategy="synchronous">

    <amqp:inbound-endpoint ref="authorisationChangeQueueDef" responseTimeout="10000" doc:name="authorisation-change-consumer" />

    <set-variable variableName="authorisationId" value="#[message.payloadAs(java.lang.String)]" doc:name="set authorisation id"/>

    <logger message="#['Authorisation Changed Event received. authorisation id: ' + flowVars.authorisationId]" level="INFO" doc:name="Logger"/>

    <http:request config-ref="Mule_Authorisation_Management_API_HTTP_Request_Configuration" path="#['/authorisation/' + flowVars.authorisationId]" method="GET" doc:name="HTTP REST call to Authorisation API"/>

    <dw:transform-message doc:name="Transform to basic owner-authorisation"  metadata:id="1bdd6f7e-6b4b-415b-819d-e88b9ff7f92b">
        <dw:input-payload mimeType="application/json"/>
        <dw:set-payload resource="dwl/owner-authorisation-skeleton.dwl"/>
    </dw:transform-message>

         <logger message="#['type before ' + message.dataType.type]" level="INFO" doc:name="Logger"/>

    <!-- Enricher targeting flowVar to keep original payload type intact -->            
    <enricher target="#[flowVars.ownerAuthorisationJsonMap]"  doc:name="Copy to JSON map"> 
        <json:json-to-object-transformer returnClass="java.util.HashMap" doc:name="JSON to Object"/>
    </enricher> 

         <logger message="#['type after: ' + message.dataType.type]" level="INFO" doc:name="Logger"/>

    <!--  subflow that uses flowVars.ownerAuthorisationJsonMap and MEL --> 
    <flow-ref name="enrich-Sub_Flow" doc:name="enrich-Sub_Flow"/>


    <dw:transform-message doc:name="Final Transform Message" metadata:id="fa7e7663-50bb-4675-a8c8-e8cb311946a9">
        <dw:input-payload mimeType="application/json"/>
        <dw:set-payload resource="classpath:dwl/owner-authorisation-enrichment.dwl"/>
    </dw:transform-message>


    <logger  level="INFO" doc:name="Logger all "/>


    <!-- Does a manual ACK to RabbitMQ, only do once we know all the processing is complete -->
    <amqp:acknowledge-message doc:name="AMQP-0-9 Acknowledge Message"/>


    <rollback-exception-strategy maxRedeliveryAttempts="3" doc:name="Rollback Exception Strategy">
        <logger message="#['REQUESTING RETRY OF REQUEST... delivery-tag : ' + message.inboundProperties['delivery-tag']]" level="INFO" doc:name="Logger"/>
        <!-- re-queue message for re-tries -->
        <amqp:reject-message requeue="true" doc:name="AMQP-0-9 Reject Message" />

        <on-redelivery-attempts-exceeded>
            <logger message="REDELIVERY EXHAUSTED" level="ERROR" doc:name="Logger"/>
            <amqp:reject-message doc:name="AMQP-0-9 Reject Message" />
        </on-redelivery-attempts-exceeded>           
    </rollback-exception-strategy>

</flow>

<sub-flow name="enrich-Sub_Flow">
    <set-variable variableName="organisationId" value="#[ownerAuthorisationJsonMap.organisationId]" doc:name="Extract Organisation Id" encoding="UTF-8" mimeType="application/json"/>

    <enricher target="#[flowVars.organisationName]" source="#[payload.organisationLatestDetail.organisationName]" doc:name="Message Enricher">  
        <processor-chain doc:name="Processor Chain">
            <flow-ref name="get-organisation-details-SubFlow" doc:name="get-organisation-details-Sub_Flow" />
           <json:json-to-object-transformer returnClass="java.util.HashMap" doc:name="JSON to Object"/>
           <logger level="INFO" message="Org Name :- Id:- #[payload.organisationLatestDetail.organisationName]" doc:name="Logger"/>
       </processor-chain>
    </enricher>
</sub-flow>

<sub-flow name="get-organisation-details-SubFlow">
    <http:request config-ref="VOA_Mule_Customer_Management_API_HTTP_Request_Configuration" path="#['/organisation?organisationId='+ flowVars.organisationId]" method="GET"
        doc:name="HTTP - Get Customer details">
    </http:request>
</sub-flow>

Dataweave 1

%dw 1.0
%output application/json
---
{
    authorisationId: payload.id,
    uarn: payload.uarn,
    authorisedParties: payload.parties map ((party , indexOfParty) -> {
        organisationId: party.authorisedPartyOrganisationId,
        status: party.authorisedPartyStatus,
        startDate: party.startDate
    }),
    endDate: payload.endDate,
    organisationId: payload.authorisationOwnerOrganisationId,
    propertyLinkSubmissionId: payload.submissionId,
    startDate: payload.startDate,
    status: payload.authorisationStatus
}

Dataweave 2(这将有进一步的映射,从一个Java地图太)

%dw 1.0
%output application/json
---
{
    authorisationId: payload.authorisationId,
    authorisedParties: payload.authorisedParties map ((authorisedParty , indexOfAuthorisedParty) -> {
        organisationId: authorisedParty.organisationId,
        startDate: authorisedParty.startDate,
        status: authorisedParty.status
    }),
    endDate: payload.endDate,
    organisationId: payload.organisationId,
    organisationName: flowVars.organisationName,
    propertyLinkSubmissionId: payload.propertyLinkSubmissionId,
    startDate: payload.startDate,
    status: payload.status,
    uarn: payload.uarn
}

日志

INFO  2017-08-10 15:05:52,787 [amqpReceiver.02] org.mule.api.processor.LoggerMessageProcessor: Authorisation Changed Event received. authorisation id: 1
INFO  2017-08-10 15:05:57,844 [amqpReceiver.02] org.mule.api.processor.LoggerMessageProcessor: type before class com.mulesoft.weave.reader.ByteArraySeekableStream
INFO  2017-08-10 15:06:02,058 [amqpReceiver.02] org.mule.api.processor.LoggerMessageProcessor: type after: class com.mulesoft.weave.reader.ByteArraySeekableStream
INFO  2017-08-10 15:06:13,309 [amqpReceiver.02] org.mule.api.processor.LoggerMessageProcessor: Org Name :- Id:- Org Name 3

Answer 1:

该问题是由JSON有效载荷流尽管它在消息被富集引起的JSON到对象的变压器被消耗,而不是被复位。 看到这个问题- https://www.mulesoft.org/jira/browse/MULE-10623 。

最简单的解决方案是重置消息后浓缩塔流:

...
<enricher target="#[flowVars.myMar]" doc:name="Message Enricher"> 
    <json:json-to-object-transformer doc:name="JSON to Object"/>
</enricher>
<expression-component doc:name="Expression">
    <![CDATA[payload.seek(0);]]>
</expression-component>
...

或有效载荷转向字符串使用消息富集之前:

...
<object-to-string-transformer doc:name="Object to String"/>
<enricher target="#[flowVars.myMar]"  doc:name="Message Enricher"> 
...

仅供参考我重复这个简单的流量问题:

<flow name="flow">

    <dw:transform-message doc:name="Transform Message">
        <dw:set-payload>
<![CDATA[%dw 1.0
%output application/json
---
{
    foo:0
}]]>
        </dw:set-payload>
    </dw:transform-message>
    <enricher target="#[flowVars.myVar]" doc:name="Message Enricher"> 
        <json:json-to-object-transformer doc:name="JSON to Object"/>
    </enricher>
    <dw:transform-message doc:name="Transform Message">
        <dw:set-payload>
<![CDATA[%dw 1.0
%output application/json
---
payload
]]> 
        </dw:set-payload>
    </dw:transform-message>
</flow>


文章来源: Mule json-to-object-transformer and Dataweaver Transformer exception
标签: mule mule-el