【问题标题】:Type mismatch error returned due to different type of messages being returned in Mule由于 Mule 中返回的消息类型不同,返回类型不匹配错误
【发布时间】:2026-02-05 18:30:01
【问题描述】:

我有一个 Mule 工作流,它可以接收响应消息或业务错误消息作为转换消息连接器的输入。

当它从转换消息连接器移动到对象到字符串时,它会遇到此错误:

ERROR 2016-10-07 18:08:40,187 [[userprocess].userprocess-httpListenerConfig.worker.01] org.mule.exception.DefaultMessagingExceptionStrategy: 
********************************************************************************
Message               : Exception while executing: 
    }) when ((payload[0].userResponse != null and payload[0].userResponse.category == "SUCCESS")) and
                         ^
Type mismatch
     found :name, :string
  required :name, :object
Type                  : com.mulesoft.weave.mule.exception.WeaveExecutionException
Code                  : MULE_ERROR--2
********************************************************************************
Exception stack is:
1. Type mismatch
     found :name, :string
  required :name, :object (com.mulesoft.weave.engine.ast.dynamic.DynamicDispatchException)
  com.mulesoft.weave.engine.ast.dynamic.DynamicDispatchNode:65 (null)
2. Exception while executing: 
    }) when ((payload[0].userResponse != null and payload[0].userResponse.category == "SUCCESS")) and
                         ^
Type mismatch
     found :name, :string
  required :name, :object (com.mulesoft.weave.mule.exception.WeaveExecutionException)
  com.mulesoft.weave.mule.WeaveMessageProcessor$WeaveOutputHandler:166 (null)
********************************************************************************
Root Exception stack trace:
com.mulesoft.weave.engine.ast.dynamic.DynamicDispatchException: Type mismatch
     found :name, :string
  required :name, :object

我认为这是因为当消息被转换消息处理时,它会根据不同的字段执行逻辑,所以我需要做的是在返回业务错误消息时忽略任何响应消息逻辑,反之亦然。

我该怎么做?

Dataweave 代码(JSON 到 XML):

%dw 1.0
%output application/xml
---
{
    (Data: {
        userId: flowVars.queryParams.userId,
        Message: "User created successfully"
    }) when (payload[0].userResponse.category == "SUCCESS") and
            (payload[1].userResponse.category == "SUCCESS"),            

    (Exception: {
        userId: flowVars.queryParams.userId,
        Message: payload[1].exception.message
        }) when (payload.exception?) and 
                (payload[1].exception.action == "createUser") and
                (payload[0].exception.code != "-1"),
    (Exception: {
       userId: flowVars.queryParams.userId,
       Message: payload[0].exception.message
       }) when (payload.exception?) and 
            (payload[0].exception.action == "amendUser") and
            (payload[1].exception.replyStatus.code != "-1"),
}

【问题讨论】:

    标签: json xml mule anypoint-studio dataweave


    【解决方案1】:

    你可以在这种情况下使用when/otherwise。你很接近,只需使用 {} 而不是括号。大致如下:

    %dw 1.0
    %input payload application/json
    %output application/xml
    ---
    {
        Data: {
            userId: flowVars.queryParams.userId,
            Message: "User created successfully"
        }
    }
    when (payload[0].userResponse.category == "SUCCESS") and
         (payload[1].userResponse.category == "SUCCESS"))
    otherwise
    {
        Exception: {
            userId: flowVars.queryParams.userId,
            Message: payload[1].exception.message
        }
    }
    

    【讨论】:

    • 嗨。即使剪切和粘贴上面的代码,我仍然会遇到同样的错误。我还需要添加第三条规则,所以我更新了上面的 dataweave 代码。谢谢
    • 你的有效载荷是什么样的? @user3165854
    • 嗨。没关系。我发现对对象的这种检查对我有用“当(有效负载 [0] .userResponse?)和......”。它检查 userResponse 对象是否存在